user2491254
user2491254

Reputation: 783

Rename unnamed column pandas dataframe

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 ''.

Upvotes: 71

Views: 186888

Answers (11)

ekraus
ekraus

Reputation: 124

This is another solution to rename the first column:

data.rename(columns={data.columns[0]:'newname'},inplace=True)

Upvotes: 0

Pedroide
Pedroide

Reputation: 11

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 )

Upvotes: 1

Sanjoy Som
Sanjoy Som

Reputation: 1

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

df.columns = df.columns.str.replace('Unnamed: 0','new_name')

Upvotes: 0

Hamad Alibrahim
Hamad Alibrahim

Reputation: 147

This should work:

data.rename( columns={0 :'Articles'}, inplace=True )

Upvotes: 11

shyam_gupta
shyam_gupta

Reputation: 328

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.

Upvotes: 2

tsveti_iko
tsveti_iko

Reputation: 7972

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'

Upvotes: 6

saccpp
saccpp

Reputation: 111

Try the below code,

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

Upvotes: 11

ambrish dhaka
ambrish dhaka

Reputation: 749

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'.

Upvotes: 14

Marco
Marco

Reputation: 653

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 )

Upvotes: 62

KyungHoon Kim
KyungHoon Kim

Reputation: 2999

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

Upvotes: 28

TomAugspurger
TomAugspurger

Reputation: 28936

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

Upvotes: 1

Related Questions