0

I have a pandas dataframe on which I did a groupby and value_counts:

df4.groupby(['City'])['User Type'].value_counts()

This gives me following output:

City  User Type 
C     Subscriber    238889
      Customer       61110
      Dependent          1
N     Subscriber    269149
      Customer       30159
W     Subscriber    220786
      Customer       79214

Now I did df6=df5.unstack(level=1).reset_index()

this returned a df like shown below:

enter image description here

Now i did df6.set_index('City',inplace=True)

This produces output:

enter image description here

I am unable to understand why city is shown under "User Type" and is there a way to remove "User Type" index name

After doing:

df6=df5.unstack(level=1).reset_index().rename_axis(None, axis=1)

and then doing : df6.set_index('City',inplace=True)

it still shows city on different level:

enter image description here

Sarthak
  • 1,076
  • 1
  • 12
  • 20

1 Answers1

1

Use del df6.columns.name, basically you need to remove the name of the columns index which happens to get the User Type name when you do unstacking.

Franco Piccolo
  • 6,845
  • 8
  • 34
  • 52