Pandas rename column by position?

PythonPandas

Python Problem Overview


I was just wondering if I can rename column names by their positions. I know how to rename them by their actual names using:

df.rename(columns = {})

How do I do it if I do not know the column names and know only their positions?

Python Solutions


Solution 1 - Python

try this

df.rename(columns={ df.columns[1]: "your value" }, inplace = True)

Solution 2 - Python

You can do this:

df.rename(columns={ df.columns[1]: "whatever" })

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJun JangView Question on Stackoverflow
Solution 1 - PythonExpratorView Answer on Stackoverflow
Solution 2 - PythonJohn ZwinckView Answer on Stackoverflow