close

How to (re)name an empty column header in a pandas dataframe without exporting to csv

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to (re)name an empty column header in a pandas dataframe without exporting to csv in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How to (re)name an empty column header in a pandas dataframe without exporting to csv?

  1. How to (re)name an empty column header in a pandas dataframe without exporting to csv?

    You can use this if there are multiple empty columns. This will generate an empty column with cols and i (for the column position)

  2. (re)name an empty column header in a pandas dataframe without exporting to csv

    You can use this if there are multiple empty columns. This will generate an empty column with cols and i (for the column position)

Method 1

I think inplace=True has to be removed, because it return None:

df2 = df1.rename(columns = {"" : "Signal"})

df1.rename(columns = {"" : "Signal"}, inplace = True)

Another solution is asign new name by position:

df.columns.values[0] = 'Signal'

Sample:

df1 = pd.DataFrame({'':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9]})

print (df1)
      B  C
0  1  4  7
1  2  5  8
2  3  6  9

df2 = df1.rename(columns = {"" : "Signal"})
print (df2)
   Signal  B  C
0       1  4  7
1       2  5  8
2       3  6  9

Method 2

You can use this if there are multiple empty columns. This will generate an empty column with cols and i (for the column position)

df.columns = ["cols_"+str(i) if a == "" else a for i, a in enumerate(df.columns)]

#cols -> just rename the column name just as you want
#i -> count the column number

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read