[英]Unexpected KeyError Pandas while trying to aggregate multiple functions into new column
我看了下面的問題:
我有數據
p.date p.instrument p.sector \
11372 2013-02-15 00:00:00 A Health Care
11373 2013-02-15 00:00:00 AA Materials
11374 2013-02-15 00:00:00 AAPL Information Technology
11375 2013-02-15 00:00:00 ABBV Health Care
11376 2013-02-15 00:00:00 ABC Health Care
p.industry p.retn p.pfwt b.bwt
11372 Health Care Equipment & Services -5.232929 NaN 0.000832
11373 Aluminum 0.328947 NaN 0.000907
11374 Computer Hardware -1.373927 NaN 0.031137
11375 Pharmaceuticals 2.756020 NaN 0.004738
11376 Health Care Distribution & Services -0.371179 NaN 0.000859
但是當我嘗試:
test1.groupby("p.sector").agg({'r1': lambda x: x['p.pfwt'].sum()})
我收到了錯誤
KeyError: 'r1'
我正在嘗試使用當前DataFrame的一組結果創建新列。
我錯過了什么? 謝謝
使用
test1.groupby("p.sector").agg({'p.pfwt': np.sum})
以這個pandas docs為例。
.agg([np.sum, np.mean, np.std]).rename(columns={'sum': 'foo', 'mean': 'bar', 'std': 'baz'}) )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.