The default behavior of pandas groupby is to turn the group by columns into 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?
question from:
https://stackoverflow.com/questions/65890743/pandas-unable-to-remove-column-from-dataframe 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…