Nagesh Joshi Nagesh Joshi - 4 months ago 22
Python Question

Groupby() function of pandas working incorrectly

When I call my data frame


This is how it looks:

Pclass Fare Survived Fare Kind Counts
0 3 7.2500 0 Lowest 1
2 3 7.9250 1 Low 1
4 3 8.0500 0 Low 1
5 3 8.4583 0 Medium 1
7 3 21.0750 0 high 1

I wanted to group my data according to Survived and Fare Kind, I used the following code

third_class_grouped =third_class.groupby(["Survived","Fare Kind"], as_index=False)["Counts"].sum()

This is the output I'm getting for


Survived Fare Kind Counts
Survived Fare Kind
0 Lowest NaN NaN NaN
Low NaN NaN NaN
Medium NaN NaN NaN
high NaN NaN NaN
1 Lowest NaN NaN NaN

How do I rectify my code to get the sums in place of NaN's in Counts columns and force the Survived and Fare kind out of index


Try something like this.

 sums = third_class.groupby(["Survived","Fare Kind"]).sum()

I find that just summing everything and then selecting the aggregation after is easier to understand.