我有一个包含列表的 pandas dataframe
,这个列表在 split_categories 列中:
df.head()
album_id categories split_categories
0 66562 480.494 [480, 494]
1 114582 128 [128]
2 4846 5 [5]
3 1709 9 [9]
4 59239 105.104 [105, 104]
我希望选择所有行,其中至少一个类别在特定列表[480、9、104]中。
预期输出:
album_id categories split_categories
0 66562 480.494 [480, 494]
3 1709 9 [9]
4 59239 105.104 [105, 104]
我使用apply
方法成功完成了它:
def match_categories(row):
selected_categories = [480, 9, 104]
result = [int(i) for i in row['split_categories'] if i in selected_categories]
return result
df['matched_categories'] = df.apply(match_categories, axis=1)
但是这段代码在生产环境中运行时间太长了(我要对包含列表的多列运行它)
有没有一种方法可以运行类似于:
df[~(df['split_categories'].anyvalue.isin([480, 9, 104]))]
谢谢
df['split_categories']
列表的最大大小是多少?例如,它总是1或2个项目吗? - jpp