Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Row Index dataframe pandas

Tags:

python

pandas

Given a df

in[0]df1
out[0]
        DATE    REVENUE    COST    POSITION
FACTOR
0    2017/01/01    1000    900    10
1    2017/01/01    900     700    9
2    2017/01/01    1100    800    7

I have an additional row FACTOR. After trying reset_index() and other ways, I cannot remove the FACTOR multi (row) index. Is there a way to do so?

I know it's common to drop columns and reset index but not this way though.

like image 504
bryan.blackbee Avatar asked May 01 '17 08:05

bryan.blackbee


2 Answers

I hope this works :)

df.reset_index(inplace=True) # Resets the index, makes factor a column
df.drop("Factor",axis=1,inplace=True) # drop factor from axis 1 and make changes permanent by inplace=True
like image 120
Mr.Pacman Avatar answered Oct 01 '22 20:10

Mr.Pacman


Try using:

df1.reset_index(drop=True)

This resets the index to the default integer index and removes the original one. If you want to assign this change to original dataframe it is easier to use:

df1.reset_index(drop=True, inplace=True)

As it will edit the df1 dataframe without making a copy of it.

like image 41
zipa Avatar answered Oct 01 '22 18:10

zipa