PANDAS Group通过删除标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35674987/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:47:17  来源:igfitidea点击:

PANDAS GroupBy Removing Header

pythonpandas

提问by Rob Jarvis

I'm using the PANDAS groupBy and noticing it is removing the header name of the value I am running it on.

我正在使用 PANDAS groupBy 并注意到它正在删除我正在运行它的值的标题名称。

data = pd.read_csv("<CSV FILE NAME>", low_memory=False)
print data.head()
print data.columns

Gives me the following output:

给我以下输出:

        Store ID        Daily Sales
0       4444444         436
1       4555555          406
2       6435353         487
3       3421456          637
4       1111111         516
Index([u'Store ID', u' Daily Sales'], dtype='object')

When I run

当我跑

data = data.groupby(['Store Number']).mean()
print data.head()
print data.columns

The output is changed to

输出更改为

                  Daily Sales
Store ID             
4166646        236.280394
4166663        152.061884
4166664        131.163746
4166665        144.920044
4166666        225.075027
Index([u'Daily Sales'], dtype='object')

The Store ID header name is being added as a value and removed from the header names. What is the reason behind this and is there a fix?

Store ID 标头名称作为值添加并从标头名称中删除。这背后的原因是什么,是否有解决方法?

回答by Alexander

set the as_index parameter to False.

将 as_index 参数设置为 False。

data.groupby(['Store Number'], as_index=False).mean()

回答by Rob Jarvis

Solved by @cel in comments adding in data = data.reset_index()after running groupby() added the header back

由@cel 在data = data.reset_index()运行 groupby() 后添加的注释中解决,将标题添加回来