【问题标题】:how to merge a multirows header of a pandas dataframe into a single cell header?如何将熊猫数据框的多行标题合并到单个单元格标题中?
【发布时间】:2017-05-06 16:44:16
【问题描述】:

我有一个来自 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

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

任何帮助将不胜感激

【问题讨论】:

    标签: python excel python-2.7 pandas


    【解决方案1】:

    您可以先通过loc选择,然后将NaN替换为fillna为空字符串并应用join。如有必要,通过str.strip 删除第一个和最后一个空格,然后通过选择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  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-21
      • 2019-01-16
      • 2022-01-12
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2020-09-26
      • 2018-11-16
      相关资源
      最近更新 更多