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?
set the as_index parameter to False.
data.groupby(['Store Number'], as_index=False).mean()
Solved by @cel in comments adding in data = data.reset_index()
after running groupby() added the header back
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With