我有一个熊猫DataFrame,并且我想要使用的列的值是列表。我想将每个列表中的两个元素逐个组合起来,并输出到另一个DataFrame中。
例如,我有数据帧df
,其中包含col_a
和col_b
。 col_b
的值是列表。我想循环df.col_b
的值,输出配对列表。熊猫循环列的值
import pandas as pd
df=pd.DataFrame({'col_a':['ast1','ast2','ast3'],'col_b':[['text1','text2','text3'],['mext1','mext2','mext3'],['cext1','cext2']]})
df
col_a col_b
0 ast1 [text1, text2, text3]
1 ast2 [mext1, mext2, mext3]
2 ast3 [cext1, cext2]
我想这一点:
col_a col_b_1
0 ast1 [text1, text2]
1 ast1 [text1, text3]
2 ast1 [text2, text3]
3 ast2 [mext1, mext2]
4 ast2 [mext1, mext3]
5 ast2 [mext2, mext3]
6 ast3 [cext1, cext2]