Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

The default behavior of pandas groupby is to turn the group by columns into the index and remove them from the list of columns of the dataframe. For instance, say I have a dataFrame with these columns

col1|col2|col3|col4

if I apply a groupby say with columns col2 and col3 this way

df.groupby(['col2','col3']).sum()

The dataframe df no longer has the ['col2','col3'] in the list of columns. They are automatically turned into the indices of the resulting dataframe.

My question is how can I perform groupby on a column and yet keep that column in the dataframe?

1 Answer

0 votes
by (107k points)

You can simply use df.groupby(), refer the following code: 

df.groupby(['col2','col3'], as_index=False).sum()

If you are interested in learning Pandas and want to become an expert in Python Programming, then check out this Python Course and upskill yourself.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...