我有一个数据框-
TransactionDT TransactionAmt TransactionHour
0 86400 68.5 0
1 86401 29.0 1
2 86469 59.0 1
3 86499 50.0 2
4 86506 50.0 3
我想创建一个新的数据框,将TransactionAmt
按TransactionHour
分组,如-
Sum(TransactionAmt) TransactionHour
0 68.5 0
1 88.0 1 (sum of those with TransactionHour == 1)
2 50.0 2
3 50.0 3
我写的代码是-
sliced_data2 = data.groupby(['TransactionHour'])['TransactionAmt'].sum()
但这只会给我Sum(TransactionHour)
答案 0 :(得分:1)
sliced_data2 = data.groupby('TransactionHour',as_index = False).agg({"TransactionAmt" : "sum"})
答案 1 :(得分:1)
sliced_data2 = data.groupby(['TransactionHour'])['TransactionAmt'].agg('sum')
这将起作用
答案 2 :(得分:1)
raw_data = {'TransactionDT': [86400, 86401, 86469, 86499, 86506],
'TransactionAmt': [68.5, 29.0, 59.0, 50.0, 50.0],
'TransactionHour': [0,1,1,2,3]}
df = pd.DataFrame(raw_data)
df.groupby('TransactionHour',as_index = False).agg({"TransactionAmt" : "sum"})
答案 3 :(得分:0)
您需要添加as_index = False`以确保您没有设置要分组为数据框新索引的列。
import pandas as pd
a = {'TransactionDT':[86400, 86401, 86469, 86499, 86506],'TransactionAmt':[68.5, 29.0, 59.0, 50.0, 50.0],'TransactionHour':[0,1,1,2,3]}
df = pd.DataFrame(a)
sliced_df = df.groupby(['TransactionHour'],as_index=False)['TransactionAmt'].sum())
print(sliced_df)
输出:
TransactionHour TransactionAmt
0 0 68.5
1 1 88.0
2 2 50.0
3 3 50.0