你要使用
transform此方法将返回索引与df对齐的
Series,然后可以将其添加为新列:
In [74]:df = pd.Dataframe({'Date': ['2015-05-08', '2015-05-07', '2015-05-06', '2015-05-05', '2015-05-08', '2015-05-07', '2015-05-06', '2015-05-05'], 'Sym': ['aapl', 'aapl', 'aapl', 'aapl', 'aaww', 'aaww', 'aaww', 'aaww'], 'Data2': [11, 8, 10, 15, 110, 60, 100, 40],'Data3': [5, 8, 6, 1, 50, 100, 60, 120]})df['Data4'] = df['Data3'].groupby(df['Date']).transform('sum')dfOut[74]: Data2 Data3 Date Sym Data40 11 5 2015-05-08 aapl 551 8 8 2015-05-07 aapl 1082 10 6 2015-05-06 aapl 663 15 1 2015-05-05 aapl 1214 110 50 2015-05-08 aaww 555 60 100 2015-05-07 aaww 1086 100 60 2015-05-06 aaww 667 40 120 2015-05-05 aaww 121
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)