【问题标题】:python combine rows in dataframe and add up valuespython组合数据框中的行并累加值
【发布时间】:2017-09-06 09:42:56
【问题描述】:

我有一个数据框:

 Type:  Volume:
 Q     10
 Q     20 
 T     10 
 Q     10
 T     20
 T     20
 Q     10

并且我想将类型 T 合并到一行中,并且仅当两个(或更多)T 连续时才将音量加起来

即到:

 Q    10
 Q    20 
 T    10 
 Q    10 
 T    20+20=40
 Q    10

有什么方法可以做到这一点吗? DataFrame.groupby 会工作吗?

【问题讨论】:

  • 这看起来可能会开始解决您的问题stackoverflow.com/a/45679091/4365003
  • 我认为这有点不同...我想合并行而不是计算它们
  • ~~那你不就用不同的聚合函数吗?~~
  • 我找不到执行此操作的聚合函数...对不起,我是 python 新手

标签: python


【解决方案1】:

我认为这会有所帮助。此代码可以处理任意数量的连续“T”,您甚至可以更改要组合的字符。我在代码中添加了 cmets 来解释它的作用。

https://pastebin.com/FakbnaCj

import pandas as pd

def combine(df):
    combined = [] # Init empty list
    length = len(df.iloc[:,0]) # Get the number of rows in DataFrame
    i = 0
    while i < length:
        num_elements = num_elements_equal(df, i, 0, 'T') # Get the number of consecutive 'T's
        if num_elements <= 1: # If there are 1 or less T's, append only that element to combined, with the same type
            combined.append([df.iloc[i,0],df.iloc[i,1]])
        else: # Otherwise, append the sum of all the elements to combined, with 'T' type
            combined.append(['T', sum_elements(df, i, i+num_elements, 1)])
        i += max(num_elements, 1) # Increment i by the number of elements combined, with a min increment of 1
    return pd.DataFrame(combined, columns=df.columns) # Return as DataFrame

def num_elements_equal(df, start, column, value): # Counts the number of consecutive elements
    i = start
    num = 0
    while i < len(df.iloc[:,column]):
        if df.iloc[i,column] == value:
            num += 1
            i += 1
        else:
            return num
    return num

def sum_elements(df, start, end, column): # Sums the elements from start to end
    return sum(df.iloc[start:end, column])

frame = pd.DataFrame({"Type":   ["Q", "Q", "T", "Q", "T", "T", "Q"],
               "Volume": [10,   20,  10,  10,  20,  20,  10]})
print(combine(frame))

【讨论】:

  • 非常感谢您的回复。请问如果我有一个超过 2 列的数据框,我该如何更改此代码,而我只想将一列的值相加而其余的保持不变?即,而不是“类型”和“音量”,我得到了“类型”、“时间”、“音量”等,我只想将“音量”的值相加
  • 当您将元素附加到组合列表 (a) 时,只需输入 df.iloc[i,col] 其中 col 是“时间”列的列索引。 combined.append([df.iloc[i,0],df.iloc[i,1]]) 变为 combined.append([df.iloc[i,0],df.iloc[i,1],df.iloc[i,2]])combined.append(['T', sum_elements(df, i, i+num_elements, 1)]) 变为 combined.append(['T', df.iloc[i,1], sum_elements(df, i, i+num_elements, 2)])
【解决方案2】:

如果您只需要部分总和,这里有一个小技巧:

import numpy as np
import pandas as pd

df = pd.DataFrame({"Type":   ["Q", "Q", "T", "Q", "T", "T", "Q"],
                   "Volume": [10,   20,  10,  10,  20,  20,  10]})
s = np.diff(np.r_[0, df.Type == "T"])
s[s < 0] = 0
res = df.groupby(("Type", np.cumsum(s) - 1)).sum().loc["T"]
print(res)

输出:

   Volume
0      10
1      40

【讨论】:

猜你喜欢
  • 2016-01-06
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 2021-04-22
  • 2017-09-30
  • 1970-01-01
  • 2019-04-28
  • 2018-09-05
相关资源
最近更新 更多