Column name shown twice in same column pandas

2528 views python

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

answered question

2 Answers


It create with second level name of MultiIndex column name of final DataFrame.

For remove need rename_axis:

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

Or set None for

df6 = df5.unstack(level=1).reset_index() = None

print (df6)
  City  Customer  Dependent  Subscriber
0    C   61110.0        1.0    238889.0
1    N   30159.0        NaN    269149.0
2    W   79214.0        NaN    220786.0

df6 = df5.unstack(level=1)
print (df6)
User Type  Customer  Dependent  Subscriber
C           61110.0        1.0    238889.0
N           30159.0        NaN    269149.0
W           79214.0        NaN    220786.0

print (
User Type

print (

And for removing both index name (City) and columns name (User Type) use :

df6 = df5.unstack(level=1) = None = None

print (df6)

   Customer  Dependent  Subscriber
C   61110.0        1.0    238889.0
N   30159.0        NaN    269149.0
W   79214.0        NaN    220786.0

posted this

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

posted this

Have an answer?


Please login first before posting an answer.