Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas rename column by position? [duplicate]

Tags:

python

pandas

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?

like image 628
Jun Jang Avatar asked May 03 '17 12:05

Jun Jang


People also ask

How do I rename a column based on position?

You can rename pandas DataFrame column name by index (position) using rename() method or by assigning column name to df. columns. values[index] .

Can pandas have duplicate column names?

Pandas, however, can be tricked into allowing duplicate column names. Duplicate column names are a problem if you plan to transfer your data set to another statistical language. They're also a problem because it will cause unanticipated and sometimes difficult to debug problems in Python.

How do I rename a column in pandas with the same name?

Method 1: Using rename() function One way of renaming the columns in a Pandas Dataframe is by using the rename() function.


2 Answers

try this

df.rename(columns={ df.columns[1]: "your value" }, inplace = True) 
like image 75
Exprator Avatar answered Oct 13 '22 17:10

Exprator


You can do this:

df.rename(columns={ df.columns[1]: "whatever" }) 
like image 40
John Zwinck Avatar answered Oct 13 '22 15:10

John Zwinck