Home > Mobile >  How to change index names of the output from df.groupby in pandas
How to change index names of the output from df.groupby in pandas

Time:08-10

I have a pd.series df['target'] and its values are 0 if malignant and 1 if benign. I want to count the number of each value and change the index name to 'malignant' and 'benign' and which return a Series named target of length 2 with integer values and index = ['malignant', 'benign'] like this;

target
malignant 212
benign 357

I tried below

target = df.groupby(['target']).size()
target.rename(index={'0': 'malignant', '1': 'benign'})

But it returns below which the index names remain the same.

target
0 212
1 357
dtype: int64

How can I fix it? Is there a better to have the count value than using .size()?

CodePudding user response:

Besides from the fact you are not redefining the dataframe, nor passing the argument inplace=True, you can do the replacement first and then use value_counts(). Also, keep in mind that if the values are numbers, then 0 != '0'.

output = target.replace({'0': 'malignant', '1': 'benign'}).value_counts()

Or, if the values are numerical:

output = target.replace({0: 'malignant', 1: 'benign'}).value_counts()

CodePudding user response:

target.rename({1: 'malignant', 2: 'benign'}).value_counts() # mapping, changes labels
  • Related