Python 循环遍历dataframe列中列表的元素,以在新列中返回列表

Python 循环遍历dataframe列中列表的元素,以在新列中返回列表,python,pandas,list,loops,Python,Pandas,List,Loops,我有一个包含列表的列的dataframe,我试图迭代dataframe中的每一行,并与该行的列表中的每个元素连接。我正在尝试编写代码,以实现“分子\物种”中显示的结果。如果您对此有任何想法,我们将不胜感激 数据帧= import pandas as pd df = pd.DataFrame({'molecule': ['a', 'b', 'c',

我有一个包含列表的列的dataframe,我试图迭代dataframe中的每一行,并与该行的列表中的每个元素连接。我正在尝试编写代码,以实现“分子\物种”中显示的结果。如果您对此有任何想法,我们将不胜感激

数据帧=

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']]
你可以试试这个

>>将熊猫作为pd导入
>>>df=pd.DataFrame({'molecule':['a',
“b”,
"c",,
“d”,
“e”],
“物种”:[[“狗”],
[‘马’、‘猪’],
[‘猫’、‘狗’],
[‘猫’、‘马’、‘猪’],
[‘鸡’、‘猪’]})
>>>df[‘分子物种’]=(df
.应用(λx:[x['分子]+''+m表示x['物种]]中的m,轴=1))
>>>df
分子种分子种
0一只[狗][一只狗]
1b[马,猪][马,猪]
2 c[猫,狗][c猫,狗]
3d[猫,马,猪][d猫,马,猪]
4 e[鸡,猪][e鸡,e猪]

熊猫>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]
使用,然后加入, 返回列表时,请附带以下内容:


熊猫<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]

另一种方法是


你可以尝试双重列表理解。在处理pandas单元格内的子列表和字符串连接时,列表理解比使用内置pandas方法快得多

df['molecule_species'] = [[mol+' '+ a_spec for a_spec in specs] 
                                      for mol, specs in zip(df.molecule, df.species)]

Out[87]:
  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并不是用来存储复杂的对象,比如列表,通常处理pandas中对象最有效的方法是远离pandas(Andy L.的解决方案)。您需要的所有信息似乎都可以在
df.explode('species')
上找到,而且这种格式更适合以后使用熊猫进行操作。需要注意的是,
pandas
版本必须大于
0.25.0
才能使用
explode
。老实说,这应该有更多的投票权。是的,apply很慢,但是使用列表的数据帧无法解决这个问题。这个解决方案比分解更快,而且简洁+1。@ALollz:我更喜欢列表理解而不是应用
apply
。但是,我同意它比爆炸更快。向上投票:)+1建议:
来自itertools进口产品,连锁店;df['molecular\u species']=[列表(chain.from\u iterable(product([first],last)),第一个,最后一个在zip中(df.molecular,df.species)]
df['molecule_species'] = [[mol+' '+ a_spec for a_spec in specs] 
                                      for mol, specs in zip(df.molecule, df.species)]

Out[87]:
  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]