Python Forum
How replace Unnamed header column with previous
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How replace Unnamed header column with previous
#2
Assuming you want to replace all columns containing the string 'Unnamed' with the string 'nan', this will do:
print(df)
df.columns = [col if not 'Unnamed' in col else 'nan' for col in df.columns]
print(df)
Output:
First Unnamed: 1 Unnamed: 2 0 A 1 3 1 B 2 4 First nan nan 0 A 1 3 1 B 2 4
As an alternative, the following yields equal results. I'm not sure whichever I prefer but both are possible.
df.columns = df.columns.map(lambda col: col if not 'Unnamed' in col else 'nan')
Reply


Messages In This Thread
RE: How replace Unnamed header column with previous - by boring_accountant - Aug-14-2019, 11:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Impute 1 if previous row of 'days' column is between 0 & 7 JaneTan 2 1,094 Dec-08-2022, 07:42 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020