【问题标题】:Iterate over all columns in pandas dataframe to split on delimiter遍历 pandas 数据框中的所有列以在分隔符上拆分
【发布时间】:2018-11-17 22:56:51
【问题描述】:

我有一个如下所示的数据框:

    name   val
0   cat    ['Furry: yes', 'Fast: yes', 'Slimy: no', 'Living: yes']
1   dog    ['Furry: yes', 'Fast: yes', 'Slimy: no', 'Living: yes']
2   snail  ['Furry: no', 'Fast: no', 'Slimy: yes', 'Living: yes']
3   paper  ['Furry: no', 'Fast: no', 'Slimy: no', 'Living: no']

对于 val 列中列表中的每个项目,我想在 ':' 分隔符上拆分项目。然后我想让 item[0] 成为列名,而 item[1] 成为该特定列的值。像这样:

    name   Furry  Fast  Slimy  Living
0   cat    yes    yes   no     yes
1   dog    yes    yes   no     yes
2   snail  no     no    yes    yes
3   paper  no     no    no     no

我尝试将 apply(pd.Series) 用于 val 列,但这仍然给我留下了许多列,我必须手动进行拆分,或者弄清楚如何迭代地遍历所有列并进行拆分。我更喜欢从零开始拆分并创建列名。知道如何实现这一目标吗?

【问题讨论】:

    标签: python list pandas dataframe split


    【解决方案1】:

    applysplit 来创建字典:

    df.val = df.val.apply(lambda x: dict([i.split(': ') for i in x]))
    

    applypd.Series 来创建列:

    df.join(df.val.apply(pd.Series)).drop('val', 1)
    
        name Furry  Fast Slimy Living
    0    cat   yes   yes    no    yes
    1    dog   yes   yes    no    yes
    2  snail    no    no   yes    yes
    3  paper    no    no    no     no
    

    【讨论】:

    • 谢谢。但我得到这个错误: ValueError: dictionary update sequence element #5 has length 1; 2 是必需的。如何修改您提供的代码以绕过此错误?
    【解决方案2】:

    pd.DataFrame 直接接受字典列表。因此,您可以通过列表推导构建数据框,然后加入。

    L = [dict(i.split(': ') for i in x) for x in df['val']]
    
    df = df[['name']].join(pd.DataFrame(L))
    
    print(df)
    
        name Fast Furry Living Slimy
    0    cat  yes   yes    yes    no
    1    dog  yes   yes    yes    no
    2  snail   no    no    yes   yes
    3  paper   no    no     no    no
    

    【讨论】:

    • @jpp 感谢代码。没有 apply(pd.Series) 看起来很有希望,因为这不是最佳的。但我收到以下错误: ValueError: dictionary update sequence element #5 has length 1; 2 是必需的。知道如何修改代码以绕过此错误吗?
    • @guru,对不起,我不确定。您可能必须给出一个最小的数据示例它演示了您如何得到错误。否则,我们可能只是猜测。
    • @jpp 道歉,我无意中分享了一些示例数据。但幸好我想通了!该列中的某些列表中有一些额外的元素,所以我删除了这些元素。然后你的代码完美运行!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 2020-09-23
    • 2021-05-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2016-12-30
    相关资源
    最近更新 更多