The column name of the column that was aggregated by the pandas groupby.

Asked 1 years ago, Updated 1 years ago, 383 views

import pandas as pd
import numpy as np
df1 = pd.DataFrame({'living things':['white bear', 'cat', 'rabbit', 'raccoon', 'momonga', 'seaser', 'ode', 'armor gold', 'armor silver'],
                    'Category': ['Fighter', 'Fighter', 'Fighter', 'Fighter', 'Fighter', 'Fighter', 'External Expert', 'Administrator', 'Administrator', ],
                    'HP': [2,50, 100, 600, 60, 72, 1000, 1000]})
df1

Sorry to bother you again.
In pandas.dataFrame, I would like to ask you about the column names when aggregating.
For df1 as above, the column name of df2 is as follows:

df2=df1[['category', 'HP']].groupby('category').mean()
df2.dtypes

Why does df2.dtypes show no category columns in the results, only HP columns?
I'd like to use the df2 results for graph display, but if I look at the category column that I'm trying to set to the x-axis, I get an error.

I am very sorry to ask you a basic question, but I would appreciate it if you could let me know.
I look forward to your kind cooperation.

python pandas

2022-10-15 09:38

1 Answers

This is because .groupby('category') indexed the specified column.

df3=df2.reset_index()# Unindexed df2

df2.reset_index().dtypes#dtypes

Or if you want to plot, you can use the value df2.index


2022-10-15 09:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.