Rename unnamed column pandas dataframe
Asked Answered
I

10

67

My csv file has no column name for the first column, and I want to rename it. Usually, I would do data.rename(columns={'oldname':'newname'}, inplace=True), but there is no name in the csv file, just ''.

Illboding answered 29/9, 2014 at 11:21 Comment(1)
How was this csv generated? If it was exported from pandas then that sometimes indicatates that the first column was the index. In that case you can tell pandas this when reading it in pd.read_csv('file.csv', index_col=[0])Labarum
L
61

You can view the current dataframe using data.head()

if that returns 'Unnamed: 0' as the column title, you can rename it in the following way:

data.rename( columns={'Unnamed: 0':'new column name'}, inplace=True )
Landowner answered 19/6, 2017 at 17:35 Comment(1)
my column has no name when I print the DataFrame it shows the column 0 only and i used this technique its not helping me, can you please suggest any other method to replace the nameBladdernut
E
28

When you load the csv, use the option 'index_col' like

pd.read_csv('test.csv', index_col=0)

index_col : int or sequence or False, default None Column to use as the row labels of the DataFrame. If a sequence is given, a MultiIndex is used. If you have a malformed file with delimiters at the end of each line, you might consider index_col=False to force pandas to not use the first column as the index (row names)

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.io.parsers.read_csv.html

Eutectoid answered 24/2, 2015 at 6:46 Comment(1)
I dont think this solves the problem. I tried it and it did notDevora
P
14

The solution can be improved as data.rename( columns={0 :'new column name'}, inplace=True ). There is no need to use 'Unnamed: 0', simply use the column number, which is 0 in this case and then supply the 'new column name'.

Professionalize answered 10/2, 2019 at 15:3 Comment(2)
With Pandas 1.0.3, renaming columns by a dictionary with index keys doesn't seem to work. It only works for rows.Neibart
In fact, I think it only works for rows because the actual DataFrame index uses sequential integers as row names.Neibart
H
11

Try the below code,

df.columns = [‘A’, ‘B’, ‘C’, ‘D’]

Hairtail answered 6/11, 2019 at 11:56 Comment(2)
The best and easiest solution!Kephart
This works in this case but its not a good solution, If there is a change in the order of columns, this solution will mess up the columns. using df.rename and specifically mentioning the old & new column names is the most reliable way of doing it.Terranceterrane
R
11

This should work:

data.rename( columns={0 :'Articles'}, inplace=True )
Rosenblast answered 6/12, 2020 at 3:25 Comment(0)
F
5

It can be that the first column/row could not have a name, because it's an index and not a column/row. That's why you need to rename the index like this:

df.index.name = 'new_name'
Furculum answered 13/2, 2020 at 12:18 Comment(2)
+1 bc I searched for a way to do this to no avail - really handy when all you want to do is copy/paste over to excel. come on python, everything should have a name by default!Paperboard
I was searching for this since days, thank you!Verdict
B
2

usually the blank column names are named based on their index

so for example lets say the 4 column is unnamed.

df.rename({'unnamed:3':'new_name'},inplace=True)

usually it is named like this since the indexing of columns start with zero.

Blavatsky answered 16/6, 2020 at 11:46 Comment(0)
F
1

It has a name, the name is just '' (the empty string).

In [2]: df = pd.DataFrame({'': [1, 2]})

In [3]: df
Out[3]: 

0  1
1  2

In [4]: df.rename(columns={'': 'A'})
Out[4]: 
   A
0  1
1  2
Frightened answered 29/9, 2014 at 11:51 Comment(3)
You need inplace=True to change df directly. df.rename(columns={'': 'A'}, inplace=True)Cavalryman
i tried to use the inplace = True, and it returned the blank dataframe, not sure what's going on, is there any other solution??Bladdernut
just incase someone needed this: in pandas 0.23.4, I need to use .rename(columns={'Unnamed: 0': 'id'}) to rename the first unnamed columnInvention
G
1

As of 2024, this numeric index form works:

df.rename( columns={0 :'new_name'}, inplace=True )

while the string one doesn't:

df.rename( columns={'Unnamed: 0':'new_name'}, inplace=True )
Ga answered 20/6, 2024 at 14:5 Comment(1)
I did data.rename( columns={'0':'new column name'}, inplace=True ) until I found this one that I cannot use column name as string, this is confusing for getting column renmaeAnikaanil
F
0

Another solution is to invoke the columns of the dataframe and use replace:

df.columns = df.columns.str.replace('Unnamed: 0','new_name')
Flautist answered 23/3, 2022 at 20:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.