pandas groupby到to_csv [英] Pandas groupby to to_csv

查看:122
本文介绍了 pandas groupby到to_csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要将Pandas groupby数据帧输出到CSV.尝试了各种StackOverflow解决方案,但是它们没有起作用.

Want to output a Pandas groupby dataframe to CSV. Tried various StackOverflow solutions but they have not worked.

Python 3.6.1,Pandas 0.20.1

Python 3.6.1, Pandas 0.20.1

groupby结果如下:

groupby result looks like:

id  month   year    count
week                
0   9066    82  32142   895
1   7679    84  30112   749
2   8368    126 42187   872
3   11038   102 34165   976
4   8815    117 34122   767
5   10979   163 50225   1252
6   8726    142 38159   996
7   5568    63  26143   582

想要一个看起来像的csv

Want a csv that looks like

week  count
0   895
1   749
2   872
3   976
4   767
5   1252
6   996
7   582

当前代码:

week_grouped = df.groupby('week')
week_grouped.sum() #At this point you have the groupby result
week_grouped.to_csv('week_grouped.csv') #Can't do this - .to_csv is not a df function. 

阅读SO解决方案:

输出groupby到csv文件熊猫

week_grouped.drop_duplicates().to_csv('week_grouped.csv')

结果: AttributeError:无法访问"DataFrameGroupBy"对象的可调用属性"drop_duplicates",请尝试使用"apply"方法

Result: AttributeError: Cannot access callable attribute 'drop_duplicates' of 'DataFrameGroupBy' objects, try using the 'apply' method

Python熊猫-将groupby输出写入文件

week_grouped.reset_index().to_csv('week_grouped.csv')

结果: AttributeError:无法访问'DataFrameGroupBy'对象的可调用属性'reset_index',请尝试使用'apply'方法"

Result: AttributeError: "Cannot access callable attribute 'reset_index' of 'DataFrameGroupBy' objects, try using the 'apply' method"

推荐答案

尝试执行以下操作:

week_grouped = df.groupby('week')
week_grouped.sum().reset_index().to_csv('week_grouped.csv')

这会将整个数据帧写入文件.如果您只想要这两列,

That'll write the entire dataframe to the file. If you only want those two columns then,

week_grouped = df.groupby('week')
week_grouped.sum().reset_index()[['week', 'count']].to_csv('week_grouped.csv')

下面是原始代码的逐行说明:

Here's a line by line explanation of the original code:

# This creates a "groupby" object (not a dataframe object) 
# and you store it in the week_grouped variable.
week_grouped = df.groupby('week')

# This instructs pandas to sum up all the numeric type columns in each 
# group. This returns a dataframe where each row is the sum of the 
# group's numeric columns. You're not storing this dataframe in your 
# example.
week_grouped.sum() 

# Here you're calling the to_csv method on a groupby object... but
# that object type doesn't have that method. Dataframes have that method. 
# So we should store the previous line's result (a dataframe) into a variable 
# and then call its to_csv method.
week_grouped.to_csv('week_grouped.csv')

# Like this:
summed_weeks = week_grouped.sum()
summed_weeks.to_csv('...')

# Or with less typing simply
week_grouped.sum().to_csv('...')

这篇关于 pandas groupby到to_csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆