如何将 pandas 数据框的多行标题合并到单个单元格标题中?

2024-01-07

我有一个来自 Excel 文件的 pandas DataFrame,其标题分为多行,如下例所示:

    0           1       2       3           4           5           6           7
5   NaN         NaN     NaN     NaN         NaN         NaN         NaN         Above
6   Planting    Harvest NaN     Flowering   Maturity    Maturity    Maturity    ground
7   date        date    Yield   date        date        date        date        biomass
8   YYYY.DDD    YYYY.DDD(kg/ha) YYYY.DDD    YYYY.DDD    YYYY.DDD    YYYY.DDD    (kg/ha)
9   NaN         NaN     NaN     NaN         NaN         NaN         NaN         NaN
10  1999.26     2000.21 5669.46 2000.14     2000.19     2000.19     2000.19     11626.7
11  2000.27     2001.22 10282.5 2001.15     2001.2      2001.2      2001.2      20565
12  2001.27     2002.22 8210.09 2002.15     2002.2      2002.2      2002.2      16509

我需要按列合并(即用空格作为粘合)第 5 至 9 行(包括),以便只有一个像这样的标题(我已格式化表格以便于阅读,因此有更多选项卡比实际应该的)

Planting date YYYY.DDD   Harvest date YYYY.DDD    Yield (kg/ha)  Flowering date YYYY.DDD     Maturity date YYYY.DDD  Maturity date YYYY.DDD  Maturity date YYYY.DDD Above ground biomass (kg/ha)
1999.262                2000.206                5669.45623      2000.138                    2000.19                 2000.19                 2000.19                 11626.73122
2000.268                2001.216                10282.49713     2001.151                    2001.2                  2001.2                  2001.2                  20564.99427
2001.272                2002.217                8210.091653     2002.155                    2002.201                2002.201                2002.201                16509.03802

我想这应该是相当微不足道的,但我找不到我的解决方案。

任何帮助将不胜感激


您可以先选择loc http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html,然后替换NaN空字符串fillna http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html并申请join。如有必要,删除第一个和最后一个空格str.strip http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.strip.html然后通过选择删除第一行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  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 pandas 数据框的多行标题合并到单个单元格标题中? 的相关文章

随机推荐