3

I would like to change my DataFrame index column with the df.set_index() function. While this provides a functional solution, it creates an "extra" row that I would like to get rid of.

df = pd.DataFrame({'A': ['a','b','c'], 'B': ['d','e','f'], 'C': [1,2,3]})

df looks like this:

   A  B  C
0  a  d  1
1  b  e  2
2  c  f  3

Changing the DataFrame index:

df.set_index('C')

Result:

   A  B
C
1  a  e
2  b  f
3  c  g

How can I make the dataframe look as follows?

C  A  B
1  a  e
2  b  f
3  c  g

I saw a similar question here but the solution using reset_index() did not provide the desired result. I would like to keep the values I have on column C and only remove the extra row.

3 Answers 3

6

If you want to have C column as index:

In [50]: r = df.set_index('C')

In [51]: r
Out[51]:
   A  B
C
1  a  d
2  b  e
3  c  f

In [52]: r.index.name
Out[52]: 'C'

In [53]: r.columns.name is None
Out[53]: True

In [54]: r = r.rename_axis(None,0).rename_axis('C',1)

In [57]: r
Out[57]:
C  A  B
1  a  d
2  b  e
3  c  f

In [55]: r.index.name is None
Out[55]: True

In [56]: r.columns.name
Out[56]: 'C'

NOTE: but it looks pretty misleading...

Sign up to request clarification or add additional context in comments.

5 Comments

that is a neat solution too.
@ScottBoston, thank you! But i think one should avoid doing such cosmetic things...
@MaxU Works perfectly, thanks for the answer! Btw, the result of rename_axis() has to be assigned to a new dataframe for it to work. Seems quite strange to me, but calling r after renaming produces the old result.
@MapleSyrup, yeah, most Pandas functions/methods don't change DF in place - they return a changed DF...
@MaxU Certainly seems so. I'm new to pandas and this behaviour is quite unintuitive...
3

Try this with [[]]:

df[['C','A','B']]

Example:

df = pd.DataFrame({'A': ['a','b','c'], 'B': ['d','e','f'], 'C': [1,2,3]})
print(df)

   A  B  C
0  a  d  1
1  b  e  2
2  c  f  3

df = df[['C','A','B']]
print(df)

   C  A  B
0  1  a  d
1  2  b  e
2  3  c  f

1 Comment

A more canonical version for n columns = df[c[-1:] + c[:-1]] where c = df.columns.tolist()
1

If you only want the DataFrame show(print) as what you want ...

print (df[['C','A','B']].to_string(index=False))
C  A  B
1  a  d
2  b  e
3  c  f

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.