我有如下列表和数据框,问题是我想搜索列表中的每个元素,如果它们存在于列中的每个列表中,则添加一个新列并将单词放入新列中。我试过了,但我的解决方案不正确。有人能帮我吗
The List :
list_m = ['KathyConWom',
'monkeyhead78',
'acorncarver',
'bonglez',
'9NewsQueensland',
'paulinedaniels',
'AdvoBarryRoux',
'_sara_jade_',
'theage',
'gaskell_mike',
'saidtarraf',
'BroHilderchump',
'jodyvance',
'COdendahl',
'pfizer',
'RobertKennedyJr',
'Real_Sheezzii',
'Kellie_Martin',
'ThatsOurWaldo',
'SCN_Nkosi',
'azsweetheart013']
数据帧名称:test
user_id text tweet_id user_name mention
22 1334471712528855040 @KathyConWom @JamesDelingpole Time to stand-up... 1362119551375314948 @KYourrights [KathyConWom, JamesDelingpole]
23 334131548 @KathyConWom @Exp_Sec_Prof It seems like weste... 1362096715877212161 @GowTolson [KathyConWom, Exp]
24 1252182507715526657 @KathyConWom I guess that the hard part would ... 1362096654514552837 @Peterpu52451065 [KathyConWom]
我想要的是:
user_id text tweet_id user_name mention new_col
22 1334471712528855040 @KathyConWom @JamesDelingpole Time to stand-up... 1362119551375314948 @KYourrights [KathyConWom, JamesDelingpole] KathyConWom
23 334131548 @KathyConWom @Exp_Sec_Prof It seems like weste... 1362096715877212161 @GowTolson [KathyConWom, Exp] KathyConWom
24 1252182507715526657 @KathyConWom I guess that the hard part would ... 1362096654514552837 @Peterpu52451065 [azsweetheart013] azsweetheart013
我尝试的是:
for index, row in df.iterrows():
for i in list_m:
i in test.mention
test["c"] = i
test
您还可以使用^{} 以列表格式获取唯一的交叉点,如下所示:
如果要将列表转换为逗号分隔的字符串,只需将其与^{} 链接,如下所示:
您也可以在^{} 中简单地使用列表理解,如下所示:
您可以使用
set
的intersection
操作来查找两个列表的公共部分要将列表转换为字符串,可以使用
试试这个
相关问题 更多 >
编程相关推荐