0

I have a dataframe which will automatically generate multiple columns like result1, result2, result3, result4 etc. I want to rename the last result i.e result4 in this case. The number of columns will be new everytime so I can't use command like

df.rename(columns={'result4': 'Final_result'}, inplace=True)

How can I do it? what command will be good to achieve this?

Chetan P
  • 177
  • 2
  • 3
  • 15

1 Answers1

1

So the issue is how to get a list of the columns so you can do [-1] to it.

Perhaps Get list from pandas DataFrame column headers which leads to:

df.rename(columns={ df.columns.tolist()[-1]: 'Final_result'}, inplace=True)

Untested.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • Thank you for replying. But I nhave multiple columns with similar name like cash1 cash2 cash3, interest1, interest2, interest3, result1, result2, result3. How can I do it then? – Chetan P Dec 09 '18 at 14:25