Skip to content Skip to sidebar Skip to footer

How To Set New Index And Remove Default Index In Pandas Df

I have attached the dataframe in the pic. In the df, subVoyageID is the default index, I am trying to remove that blank row next to the subvoyageID, so that all the column names ar

Solution 1:

Use reset_index for convert index values to column and if necessary rename column:

df = df.reset_index().rename(columns={'subVoyageID':'SVID'})

Solution 2:

That's because subVoyageID isn't a column, it is your index. Just use reset_index() to make it an actual column.

Example

>>>df

         a  b  c
myindex         
0        0  1  2
1        3  4  5
2        6  7  8

>>>df.reset_index().rename(columns={df.index.name: 'not my index'})

   not my index  a  b  c
0             0  0  1  2
1             1  3  4  5
2             2  6  7  8

Post a Comment for "How To Set New Index And Remove Default Index In Pandas Df"