14

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.

3 Answers 3

20

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
Sign up to request clarification or add additional context in comments.

Comments

13

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.

Comments

6

FACTOR is the name of the index - you shouldn't worry about it - it doesn't affect your data:

In [78]: df
Out[78]:
              DATE  REVENUE  COST  POSITION
FACTOR
10      2017/01/01     1000   900        10
11      2017/01/01      900   700         9
12      2017/01/01     1100   800         7

In [79]: df.index.name
Out[79]: 'FACTOR'

If you want to rename it or to get rid of it (preserving the index values) you can use DataFrame.rename_axis() method:

In [80]: df = df.rename_axis(None)

In [81]: df
Out[81]:
          DATE  REVENUE  COST  POSITION
10  2017/01/01     1000   900        10
11  2017/01/01      900   700         9
12  2017/01/01     1100   800         7

In [82]: df.index.name is None
Out[82]: True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.