1 回答
TA贡献1860条经验 获得超9个赞
嗯,我无法直接对 pandas 执行此操作,我只有字典的解决方案。我认为您的示例结果有错误,我认为我的结果是预期的结果。
import pandas as pd
# Prework to get your data
data = {0: [[4, 7, 3, 5], [8, 2, 6], [9, 1, 8, 2], [3, 5], [9, 4, 8], [0, 1, 4]],
1: [[4, 7, 3, 5], [8, 2, 6], [9, 1, 8, 2], [3, 5], [9, 4, 8], [0, 1, 4]]}
data2 = {0: [[2, 3, 6, 9], [2, 3, 6, 9], [2, 3, 6, 9], [2, 3, 6, 9], [2, 3, 6, 9], [2, 3, 6, 9]],
1: [[6, 2, 3, 5], [6, 2, 3, 5], [6, 2, 3, 5], [6, 2, 3, 5], [6, 2, 3, 5], [6, 2, 3, 5]]}
df = pd.DataFrame(data)
df2 = pd.DataFrame(data2)
# ---
dc = df.to_dict()
dc2 = df2.to_dict()
new_dc = dc.copy()
for key in dc:
for val in dc[key]:
new_dc[key][val] = len(set(dc[key][val]).intersection(dc2[key][val]))
new_df = pd.DataFrame(new_dc)
print(new_df)
输出:
0 1
0 1 2
1 2 2
2 2 1
3 1 2
4 1 0
5 0 0
添加回答
举报