pandas 错误 'AttributeError:'DataFrameGroupBy' 对象没有属性'而数据帧上的 groupby 功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46534653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error 'AttributeError: 'DataFrameGroupBy' object has no attribute' while groupby functionality on dataframe
提问by Arvinth Kumar
I have a dataframe news_count
. Here are its column names, from the output of news_count.columns.values
:
我有一个数据框news_count
。以下是它的列名,来自 的输出news_count.columns.values
:
[('date', '') ('EBIX UW Equity', 'NEWS_SENTIMENT_DAILY_AVG') ('Date', '')
('day', '') ('month', '') ('year', '')]
I need to groupby
by year and month and sum values of 'NEWS_SENTIMENT_DAILY_AVG'
. Below is code I tried, but neither work:
我需要groupby
按年和月计算'NEWS_SENTIMENT_DAILY_AVG'
. 下面是我试过的代码,但都不起作用:
Attempt 1
尝试 1
news_count.groupby(['year','month']).NEWS_SENTIMENT_DAILY_AVG.values.sum()
'AttributeError: 'DataFrameGroupBy' object has no attribute'
Attempt 2
尝试 2
news_count.groupby(['year','month']).iloc[:,1].values.sum()
AttributeError: Cannot access callable attribute 'iloc' of 'DataFrameGroupBy' objects, try using the 'apply' method
Input data:
输入数据:
ticker date EBIX UW Equity month year
field NEWS_SENTIMENT_DAILY_AVG
0 2007-05-25 0.3992 5 2007
1 2007-11-06 0.3936 11 2007
2 2007-11-07 0.2039 11 2007
3 2009-01-14 0.2881 1 2014
回答by SRG
extract required columns from dataframe in news_count_res
variable and then apply aggregation function
从news_count_res
变量中的数据框中提取所需的列,然后应用聚合函数
news_count_res = news_count[['year','month','NEWS_SENTIMENT_DAILY_AVG']]
news_count_res.group(['year','month']).sum()
回答by SRG
We can apply aggregation functions on required columns without extracting columns as well, as follows:
我们也可以在不提取列的情况下对所需列应用聚合函数,如下所示:
news_count.group(['year','month'])['NEWS_SENTIMENT_DAILY_AVG'].sum()