我是一名有用的助手,会将文本翻译为中文。
我有一个数据框 user_df,包含大约500,000行数据,格式如下:
我还有一个列表,名称为other_ids_that_clicked,其中包含约5,000个其他id的完整列表:
我正在检查的方法是对于user_df中的每一行,循环遍历other_ids_that_clicked。
我有一个数据框 user_df,包含大约500,000行数据,格式如下:
| id | other_ids |
|------|--------------|
| 1 |['abc', efg'] |
| 2 |['bbb'] |
| 3 |['ccc', 'ddd']|
我还有一个列表,名称为other_ids_that_clicked,其中包含约5,000个其他id的完整列表:
['abc', 'efg', 'ccc']
我希望通过在df中添加另一列来使用user_df来去重其他点击的id,当other_ids中的值在user_df ['other_ids']中时:
| id | other_ids | clicked |
|------|--------------|-----------|
| 1 |['abc', efg'] | 1 |
| 2 |['bbb'] | 0 |
| 3 |['ccc', 'ddd']| 1 |
我正在检查的方法是对于user_df中的每一行,循环遍历other_ids_that_clicked。
def otheridInList(row):
isin = False
for other_id in other_ids_that_clicked:
if other_id in row['other_ids']:
isin = True
break
else:
isin = False
if isin:
return 1
else:
return 0
这个太费时间了,我正在寻找最佳方法的建议。
谢谢!
apply(pd.Series)
时,你也可以使用pd.DataFrame(...tolist())
来达到同样的效果...几乎总是如此!这就像在pandas食谱中的一个配方。 - cs95apply
是一个被吹嘘的循环,而调用构造函数要快得多,因为它背后的代码更强大...哈哈,你可能已经知道这一点了。 - cs95