我有一个df,其中一列包含字符串列表,如下所示:
'Name' 'Method'
1 foo ['car', 'truck', 'transportation::plane']
2 bar ['car', 'transportation::helicopter', 'boat']
3 baz ['transportation::car', 'helicopter', 'boat']
我只想将列表中的项保存在包含“:”的方法下,这样我就可以得到如下结果:
'Name' 'Method'
1 foo ['transportation::plane']
2 bar ['transportation::helicopter']
3 baz ['transportation::car']
我知道我可以做一个for循环来遍历每个列表,然后使用列表理解,但是我觉得一定有一个方法不需要使用for循环。我试了以下方法
for j in range(len(df['Method'])):
df['Method'].iloc[j] = [x for x in df['Method'].iloc[j] if "::" in x]
跑起来比我想的要长得多。你知道吗
或者可以使用
str.contains
使用
apply
详细信息
或者,使用
filter
相关问题 更多 >
编程相关推荐