简体   繁体   English

带有lambda参数的pandas groupby

[英]pandas groupby with a lambda parameter

I can't understand the code: 我不明白代码:

pivot = pd.pivot_table(subset, values='count', rows=['date'], cols=['sample'], fill_value=0)

by = lambda x: lambda y: getattr(y, x)

grouped = pivot.groupby([by('year'),by('month')]).sum()

subset in the code is a DataFrame which have a column named "date"(eg2013-02-04 06:20:49.634244), and do not have a column named "year" and "month". 代码中的subset是一个DataFrame,它具有一个名为“日期”的列(例如,2013-02-04 06:20:49.634244),而没有一个名为“年”和“月”的列。

where I have trouble with 我有麻烦的地方

  • I can't figure out the "year" and "month" in: 我无法确定“年”和“月”的形式:

     grouped = pivot.groupby([by('year'),by('month')]).sum() 
  • What the meaning of 是什么意思

     grouped = pivot.groupby([by('year'),by('month')]).sum() 

What I have done: 我做了什么:

  • In the pandas pandas document says: the first parame of the pandas.DataFrame.groupby can be 在pandas中pandas文档说:pandas.DataFrame.groupby的第一个参数可以是

    by : mapping function / list of functions, dict, Series, or tuple / 作者:映射函数/函数列表,字典,系列或元组/

  • by = lambda x: lambda y: getattr(y, x) 由= lambda x:lambda y:getattr(y,x)

means by('bar') returns a function that returns the attribute 'bar' from an object 表示by('bar')返回一个函数,该函数从对象返回属性'bar'

If a callable is passed to groupby , it is called on the DataFrame 's index, so this code is is grouping by the year and month of a datetimelike index. 如果将callable传递给groupby ,则会在DataFrame的索引上调用它,因此此代码DataFrame datetimelike索引的年和月进行分组。

In [55]: df = pd.DataFrame({'a': 1.0}, 
                           index=pd.date_range('2014-01-01', periods=13, freq='M'))

In [56]: df.groupby([by('year'), by('month')]).sum()
Out[56]: 
           a
2014 1   1.0
     2   1.0
     3   1.0
     4   1.0
     5   1.0
     6   1.0
     7   1.0
     8   1.0
     9   1.0
     10  1.0
     11  1.0
     12  1.0
2015 1   1.0

More explicitly 更明确地

In [57]: df.groupby([df.index.year, df.index.month]).sum()
Out[57]: 
           a
2014 1   1.0
     2   1.0
     3   1.0
     4   1.0
     5   1.0
     6   1.0
     7   1.0
     8   1.0
     9   1.0
     10  1.0
     11  1.0
     12  1.0
2015 1   1.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM