【问题标题】:Creating new rows from single cell strings in pandas dataframe从熊猫数据框中的单个单元格字符串创建新行
【发布时间】:2020-06-25 03:36:54
【问题描述】:

我有一个 pandas 数据框,其输出直接从 USDA 文本文件中提取。以下是数据框的示例:

Date       Region                 CommodityGroup                    InboundCity  Low    High   
    1/2/2019   Mexico Crossings       Beans,Cucumbers,Eggplant,Melons   Atlanta      4500   4700
    1/2/2019   Eastern North Carolina Apples and Pears                  Baltimore    7000   8000
    1/2/2019   Michigan               Apples                            Boston       3800   4000

我正在寻找一种程序化的解决方案来分解“CommodityGroups”列中的多个商品(每个商品用逗号或上表中的“and”分隔)单元格,为分隔的商品创建新行,并复制每个新行的其余列数据。所需的示例输出:

Date       Region                    CommodityGroup     InboundCity     Low     High
    1/2/2019   Mexico Crossings          Beans              Atlanta         4500    4700
    1/2/2019   Mexico Crossings          Cucumbers          Atlanta         4500    4700
    1/2/2019   Mexico Crossings          Eggplant           Atlanta         4500    4700
    1/2/2019   Mexico Crossings          Melons             Atlanta         4500    4700
    1/2/2019   Eastern North Carolina    Apples             Baltimore       7000    8000
    1/2/2019   Eastern North Carolina    Pears              Baltimore       7000    8000
    1/2/2019   Michigan                  Apples             Boston          3800    4000

非常感谢您在此追求中提供的任何指导!

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:
    • 使用.str.split',| and ' 模式拆分列,即','' and ''|'OR
    • 使用.explode 将列表元素分成单独的行
      • (可选)在爆炸后使用.reset_index(drop=True),具体取决于您的需要。
        • df = df.explode('CommodityGroup').reset_index(drop=True)
    import pandas as pd
    
    # data
    data = {'Date': ['1/2/2019', '1/2/2019', '1/2/2019'],
            'Region': ['Mexico Crossings', 'Eastern North Carolina', 'Michigan'],
            'CommodityGroup': ['Beans,Cucumbers,Eggplant,Melons', 'Apples and Pears', 'Apples'],
            'InboundCity': ['Atlanta', 'Baltimore', 'Boston'],
            'Low': [4500, 7000, 3800],
            'High': [4700, 8000, 4000]}
    
    # create the dataframe
    df = pd.DataFrame(data)
    
    # split the CommodityGroup strings
    df.CommodityGroup = df.CommodityGroup.str.split(',| and ')
    
    # explode the CommodityGroup lists
    df = df.explode('CommodityGroup')
    
    # final
           Date                  Region CommodityGroup InboundCity   Low  High
    0  1/2/2019        Mexico Crossings          Beans     Atlanta  4500  4700
    0  1/2/2019        Mexico Crossings      Cucumbers     Atlanta  4500  4700
    0  1/2/2019        Mexico Crossings       Eggplant     Atlanta  4500  4700
    0  1/2/2019        Mexico Crossings         Melons     Atlanta  4500  4700
    1  1/2/2019  Eastern North Carolina         Apples   Baltimore  7000  8000
    1  1/2/2019  Eastern North Carolina          Pears   Baltimore  7000  8000
    2  1/2/2019                Michigan         Apples      Boston  3800  4000
    

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      df = df.set_index(['Date', 'Region', 'InboundCity', 'Low', 'High'])
         .apply(lambda x: x.str.split(',| and ').explode())
         .reset_index() 
      print(df)
      
             Date                  Region InboundCity   Low  High CommodityGroup
      0  1/2/2019        Mexico Crossings     Atlanta  4500  4700          Beans
      1  1/2/2019        Mexico Crossings     Atlanta  4500  4700      Cucumbers
      2  1/2/2019        Mexico Crossings     Atlanta  4500  4700       Eggplant
      3  1/2/2019        Mexico Crossings     Atlanta  4500  4700         Melons
      4  1/2/2019  Eastern North Carolina   Baltimore  7000  8000         Apples
      5  1/2/2019  Eastern North Carolina   Baltimore  7000  8000          Pears
      6  1/2/2019                Michigan      Boston  3800  4000         Apples
      

      【讨论】:

      • 它需要在拆分中 and 周围有空格。注意 Apples 有一个尾随空格,Pears 有一个前导空格。
      猜你喜欢
      • 2018-11-03
      • 2017-10-28
      • 1970-01-01
      • 2017-01-31
      • 1970-01-01
      • 2022-01-12
      • 2021-12-24
      • 2020-09-21
      相关资源
      最近更新 更多