0

How do i rename a column in pandas created from a multirow header?

I've attempted

df = pd.read_excel(filepath,header=[0,1])
df.rename(
    columns={"col name, line 2":"New_Column_name"}
          ,inplace=True)

# Also attempted

df.rename(columns={df.columns[0]: 'New_Column_name'},inplace=True)

Neither of those result in a renamed column, old name persists

mapping dom
  • 1,737
  • 4
  • 27
  • 50

1 Answers1

0

Loading data this way created a multindex so python couldn't find the object using index, for named items this worked for me.

df.rename(
    columns={"col name":"New_Column_name"}
          ,inplace=True)

Flattening the hierachical index as this answer and then renaming was the better solution

mapping dom
  • 1,737
  • 4
  • 27
  • 50