gpt4 book ai didi

python合并数据框中的行并添加值

转载 作者:太空狗 更新时间:2023-10-30 00:12:58 24 4
gpt4 key购买 nike

我有一个数据框:

 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 会工作吗?

最佳答案

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

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))

关于python合并数据框中的行并添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46059157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com