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?

1 Answer 1

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.

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

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.