While this question had relative answers, I couldn't find an efficient and robust way of doing it. I have to shift the last column in my data frame to the first without disturbing any of the other columns. How should I do this ? Thanks in advance.
Asked
Active
Viewed 3.3k times
1 Answers
14
cols = list(df.columns)
cols = [cols[-1]] + cols[:-1]
df = df[cols]
Fred
- 403
- 3
- 9
-
Forgot the `list`. Edited – Fred Sep 24 '18 at 12:26
-
this puts the column in the fifth place and not at the first when I ran this code snippet. – VishwaV Sep 24 '18 at 12:29
-
1That's weird. This should work for any regular data frame. I can't help you out without further details then – Fred Sep 24 '18 at 12:50
-
Wait, let me post my notebook cloudlink. – VishwaV Sep 24 '18 at 12:53
-
Works as expected. – gies0r Mar 02 '20 at 18:59