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

31,464

Solution 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

Solution 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
Share:
31,464
sudonym
Author by

sudonym

Updated on March 23, 2021

Comments

  • sudonym
    sudonym about 3 years

    I have a pandas dataframe df1 with an index column and an unnamed series of values. I want to assign a name to the unnamed series.

    The only way to do this that I know so far is to export to df1.csv using:

    df1.to_csv("df1.csv", header = ["Signal"])

    and then re-import using:

    pd.read_csv("df1.csv", sep=",")

    However, this costs time and storage space. How to do this in-memory?

    When I do df2 = df1.rename(columns = {"" : "Signal"}, inplace = True)

    I yield:

    AttributeError: "Series" object has no attribute "Signal".

  • Victor Callegari
    Victor Callegari over 4 years
    This is a simplistic solution to when there is only one single unnamed column. What is the solution when there are more than one column without a name? How do you rename them?
  • jezrael
    jezrael over 4 years
    @VictorCallegari - Then simpliest is set new columns name by list like df.columns = ['col1','col2','col3','col4'].
  • Victor Callegari
    Victor Callegari over 4 years
    I guess I could do that for now since I am dealing with less than 10 columns. But I wonder how do you handle it when you have a lot of columns where listing all of them seems to be odd.
  • jezrael
    jezrael over 4 years
    @VictorCallegari - then solution should be create list of columns like L = df.columns.tolist(), then processing list like you need, e.g. L[0] = 'col4', L[5] = 'col5' and last assign back like df.columns = L