How To Merge A Multirows Header Of A Pandas Dataframe Into A Single Cell Header?
I have a pandas DataFrame from an excel file with the header split in multiple rows as the following example: 0 1 2 3 4 5 6
Solution 1:
You can first select by loc
, then replace NaN
to empty string by fillna
and apply join
. If necessary remove first and last whitespaces by str.strip
and then remove first rows by selecting df.loc[10:]
:
df.columns = df.loc[5:9].fillna('').apply(' '.join).str.strip()
#if need monotonic index (0,1,2...) add reset index
print (df.loc[10:].reset_index(drop=True))
Planting date YYYY.DDD Harvest date YYYY.DDD(kg/ha) Yield YYYY.DDD \
0 1999.26 2000.21 5669.46
1 2000.27 2001.22 10282.5
2 2001.27 2002.22 8210.09
Flowering date YYYY.DDD Maturity date YYYY.DDD Maturity date YYYY.DDD \
0 2000.14 2000.19 2000.19
1 2001.15 2001.2 2001.2
2 2002.15 2002.2 2002.2
Maturity date (kg/ha) Above ground biomass
0 2000.19 11626.7
1 2001.2 20565
2 2002.2 16509
Post a Comment for "How To Merge A Multirows Header Of A Pandas Dataframe Into A Single Cell Header?"