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
#4
The following function should do the trick. This won't win any beauty prize however as I just quickly hacked this together.
def repeat_cols(columns):
    last_non_unnamed = '' if 'unnamed' in columns[0].lower() else columns[0]
    new_columns = []
    new_column = ''
    for column in columns:
        if 'unnamed' in column.lower():
            new_column = last_non_unnamed
        else:
            new_column = column
            last_non_unnamed = new_column
        new_columns.append(new_column)
    return new_columns

df.columns = repeat_cols(df.columns)
Reply


Messages In This Thread
RE: How replace Unnamed header column with previous - by boring_accountant - Aug-15-2019, 05:10 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,052 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