Home > Mobile >  Unnamed columns - rename - pandas
Unnamed columns - rename - pandas

Time:03-03

I am trying to rename unnamed columns in my data frame. Value of the 1st row in this column is expected to become a name of that column. If a column doesn't contain Unnamed, its name should remain unchanged.

I try to achieve it this way:

    for col in columns:
     if 'Unnamed' in col:
       df = df.rename(columns=df.iloc[0])
       break

In this case each column is renamed. Any ideas what am I doing wrong?

CodePudding user response:

Use Index.where with str.contains, it replace if False, so inverted mask by ~:

df = pd.DataFrame({'Unnamed 1':['a', 2], 'col':['b',8]})
    
df.columns = df.columns.where(~df.columns.str.contains('Unnamed'), df.iloc[0])
print (df)
   a col
0  a   b
1  2   8

Your solution is possible change by loop Series with first row:

new = []
for col, v in df.iloc[0].items():
    if 'Unnamed' in col:
        new.append(v)
    else:
        new.append(col)
df.columns = new

Same in list comprehension:

df.columns = [v if 'Unnamed' in col else col for col, v in df.iloc[0].items()]

CodePudding user response:

You can rename unamed columns as shown below

df = df.rename({'Unnamed: 0':'NewName1'})

If multiple unnamed columns then use index based on occurance of unnamed column

df = df.rename({'Unnamed: 0':'NewName1','Unnamed: 1':'NewName2'})
  • Related