python - 循环遍历 Pandas 数据框列中的列表元素以返回新列中的列表

标签 python pandas list loops

我有一个包含列表的列的数据框,我试图遍历数据框中的每一行并与该行列表的每个元素连接。我正在尝试编写代码来实现“molecule_species”中显示的结果。对此的任何想法将不胜感激。

数据框 =

import pandas as pd
df = pd.DataFrame({'molecule': ['a',
                                'b',
                                'c',
                                'd',
                                'e'],
                   'species' : [['dog'],
                                ['horse','pig'],
                                ['cat', 'dog'],
                                ['cat','horse','pig'],
                                ['chicken','pig']]})

我试图通过迭代行和列表元素来创建新列,将“分子”与“物种”中包含的列表中的每个元素连接起来。
df['molecule_species'] = [['a dog'],
                          ['b horse','b pig'],
                          ['c cat', 'c dog'],
                          ['d cat','d horse','d pig'],
                          ['e chicken','e pig']]

最佳答案

Pandas > 0.25.0

使用 Series.explode 然后 join ,
返回列表 GroupBy.agg :

df['molecule_species'] = (df.explode('species')
                            .apply(' '.join,axis=1)
                            .groupby(level=0)
                            .agg(list) )
print(df)

  molecule            species         molecule_species
0        a              [dog]                  [a dog]
1        b       [horse, pig]         [b horse, b pig]
2        c         [cat, dog]           [c cat, c dog]
3        d  [cat, horse, pig]  [d cat, d horse, d pig]
4        e     [chicken, pig]       [e chicken, e pig]

Pandas < 0.25.0
df['molecule_species']=(df.reindex(df.index.repeat(df.species.str.len()))
                          .assign(species=np.concatenate(df.species.values))
                          .apply(' '.join,axis=1)
                          .groupby(level=0)
                          .agg(list) )
print(df)
  molecule            species         molecule_species
0        a              [dog]                  [a dog]
1        b       [horse, pig]         [b horse, b pig]
2        c         [cat, dog]           [c cat, c dog]
3        d  [cat, horse, pig]  [d cat, d horse, d pig]
4        e     [chicken, pig]       [e chicken, e pig]

另一种方法是 Series.str.cat
df2 = df.explode('species')
df['molecule_species']=df2['molecule'].str.cat(df2['species'],sep=' ').groupby(level=0).agg(list)

关于python - 循环遍历 Pandas 数据框列中的列表元素以返回新列中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59776345/

相关文章:

c# - 尝试将列表转换为数组会产生 “error CS1503: Argument 1: cannot convert from ' System.Collections.Generic.List <int >' to ' System.Array'”

c - C 中的 2 元素数组列表

Python - ctypes - 如何调用函数和访问结构字段?

python - py.test : How to generate tests from fixtures

python - 我在做模拟电视

python - 在大循环 Pandas to_csv 中优化时间

python - Python Pandas 中的数据提取

list - 似乎无法理解 'list difference' (\\) 运算符

python - 类型错误 : predict() takes 2 positional arguments but 3 were given

python - 如何在另一个 python 文件中运行我的 python 文件?