繁体   English   中英

迭代pandas数据帧中的所有列以拆分分隔符

[英]Iterate over all columns in pandas dataframe to split on delimiter

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

    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列,但是这仍然留给我许多列,我必须手动进行拆分,或者弄清楚如何迭代遍历所有列并进行拆分。 我更喜欢从零开始分割并创建列名。 知道我怎么能做到这一点?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM