Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PANDAS GroupBy Removing Header

Tags:

python

pandas

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

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?

like image 870
Rob Jarvis Avatar asked Feb 27 '16 20:02

Rob Jarvis


2 Answers

set the as_index parameter to False.

data.groupby(['Store Number'], as_index=False).mean()
like image 58
Alexander Avatar answered Sep 30 '22 08:09

Alexander


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

like image 21
Rob Jarvis Avatar answered Sep 30 '22 07:09

Rob Jarvis