1

I have a data-frame (df) that looks like:

3 IDENTIFIER   DEPARTMENT  
4      12233    MARKETING  
5      15389      FINANCE
6      12237    MARKETING
7      10610          OPS
8       6493          OPS

Is there a way to remove the index from the header and re-index from 0 please?

So the resulting data-frame looks like:

  IDENTIFIER   DEPARTMENT  
0      12233    MARKETING  
1      15389      FINANCE
2      12237    MARKETING
3      10610          OPS
4       6493          OPS

I have tried using without success:

 df.reset_index()

2 Answers 2

2

First check:

print (df.index.name)
None

print (df.columns.name)
3

And then set columns names to None with DataFrame.rename_axis and DataFrame.reset_index:

df = df.rename_axis(None, axis=1).reset_index(drop=True)
print (df)
   IDENTIFIER DEPARTMENT
0       12233  MARKETING
1       15389    FINANCE
2       12237  MARKETING
3       10610        OPS
4        6493        OPS

Or:

df.columns.name = None
df = df.reset_index(drop=True)
print (df)
   IDENTIFIER DEPARTMENT
0       12233  MARKETING
1       15389    FINANCE
2       12237  MARKETING
3       10610        OPS
4        6493        OPS

Or:

df.columns.name = None
df.reset_index(drop=True, inplace=True)
print (df)
   IDENTIFIER DEPARTMENT
0       12233  MARKETING
1       15389    FINANCE
2       12237  MARKETING
3       10610        OPS
4        6493        OPS
Sign up to request clarification or add additional context in comments.

Comments

2

agree with @jezrael

or you can set inplace=True

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

1 Comment

Please dont change answer by my solution posted before.

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.