Quantcast
Channel: Active questions tagged header - Stack Overflow
Viewing all articles
Browse latest Browse all 646

Changing a dataframe headers, but keeping the previous headers as a row

$
0
0

I have two issues on a dataframe:

  • It does not have the correct headers
  • The current headers contain values that should be a "simple" (first) row of the dataframeHow do I keep my current header as the first row of my dataframe and implement correct names for my headers ?

My current solution consists of 4 steps :

  • Define the list of the expected headers : ref_columns = ['column 1', 'column2']
  • Use list comprehension and a dataframe constructor to create a dataframe from my original dataframe containing only one row (the old headers) and with the correct headers (header dataframe) : df_header_row = pd.DataFrame([list(df_original.columns)], columns=ref_columns)
  • Force the original dataframe's headers to the expected ones : df_original.columns = ref_columns
  • Concate the header dataframe and the original dataframe : df_full = concat([df_header_row, df_original], ignore_index=True)

It is definitely not a fluid solution and requires to recreate a full dataframe (header dataframe). Does anyone have a better solution?

Thanks


Viewing all articles
Browse latest Browse all 646

Trending Articles