我有一个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]
并且运行时间比我想要的要长。
答案 0 :(得分:2)
使用apply
In [220]: df.Method.apply(lambda x: [v for v in x if '::' in v])
Out[220]:
1 [transportation::plane]
2 [transportation::helicopter]
3 [transportation::car]
详细
In [222]: df['NMethod'] = df.Method.apply(lambda x: [v for v in x if '::' in v])
In [223]: df
Out[223]:
Name Method NMethod
1 foo [car, truck, transportation::plane] [transportation::plane]
2 bar [car, transportation::helicopter, boat] [transportation::helicopter]
3 baz [transportation::car, helicopter, boat] [transportation::car]
或者,使用filter
In [225]: df.Method.apply(lambda x: filter(lambda v: '::' in v, x))
Out[225]:
1 [transportation::plane]
2 [transportation::helicopter]
3 [transportation::car]
Name: Method, dtype: object
答案 1 :(得分:0)
或者您可以使用str.contains
from itertools import compress
import pandas as pd
df['Method'].apply(lambda x :list(compress(x,pd.Series(x).str.contains('::').tolist())))