Louis Law :
import pandas as pd
matrix = [(222, 34, 23),
(333, 31, 11),
(444, 16, 21),]
df_recommend = pd.DataFrame(matrix, columns=list('abc'))
df_recommend.index = ['apple','banana','cat']
matrix2 = [(222, 35, 23,99,60),
(333, 31, 20, 1,2),
(444,0,21,35,23)]
df_truth = pd.DataFrame(matrix2, columns=list('abcde'))
df_truth.index = ['apple','banana','cat']
With the 2 dataframe above, for each rows in df_recommend, I want to check if the items appear in another dataframe (df_truth)
df_recommend.apply(lambda x: x.isin(df_truth.loc[x.name]),1).astype(int)
a b c
apple 1 0 1
banana 1 1 0
cat 1 0 1
I wonder if there is more efficient way than above
jezrael :
I believe you need if want compare elementwise with matched rows by DataFrame.reindex
and compare by DataFrame.eq
:
df1 = df_truth.reindex(index=df_recommend.index, columns=df_recommend.columns)
m = df_recommend.eq(df1).astype(int)
print (m)
a b c
apple 1 0 1
banana 1 1 0
cat 1 0 1
Another idea with DataFrame.isin
:
m = df_recommend.isin(df_truth).astype(int)
m = df_recommend.apply(lambda x: x.isin(df_truth.loc[x.name]),1).astype(int)
print (m)
a b c
apple 1 0 1
banana 1 1 0
cat 1 0 1
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=279303&siteId=1