How To (re)name An Empty Column Header In A Pandas Dataframe Without Exporting To Csv
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
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
014712582369
df2 = df1.rename(columns = {"" : "Signal"})
print (df2)
Signal B C
014712582369
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 inenumerate(df.columns)]
#cols -> just rename the column name just as you want#i -> count the column number
Post a Comment for "How To (re)name An Empty Column Header In A Pandas Dataframe Without Exporting To Csv"