Unexpected KeyError Pandas when trying to aggregate multiple functions into a new column

I considered the following question:

Apply multiple functions to multiple group columns

and I have line data

                    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 

but when trying:

test1.groupby("p.sector").agg({'r1': lambda x: x['p.pfwt'].sum()})

I get an error

KeyError: 'r1'

I am trying to create new columns with a result set from the current DataFrame.

What am I missing? Thanks

+4
source share
1 answer

using

test1.groupby("p.sector").agg({'p.pfwt': np.sum})

see this pandas docs for example.

  • . , "r1", , .
  • , : .agg([np.sum, np.mean, np.std]).rename(columns={'sum': 'foo', 'mean': 'bar', 'std': 'baz'}) )
+6

All Articles