Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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 )

Solution 2:

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

Solution 3:

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

Solution 4:

This should work:

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

Solution 5:

Try the below code,

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

Post a Comment for "Rename Unnamed Column Pandas Dataframe"