[英]To check if few values in dataframe column exists in another dataframe column
I am trying to check if values present in df1 column is present in df2 column.我正在尝试检查 df1 列中存在的值是否存在于 df2 列中。 df2 contains more values than df1 and cant use
for
loop df2 包含比 df1 更多的值并且不能使用
for
循环
import pandas as pd
df1 = pd.DataFrame({'one': [2,4,6,8]})
df2 = pd.DataFrame({'one': [4,2,6,8,10]})
print(df1.isin(df2))
expected results预期成绩
one
0 True
1 True
2 True
3 True
actual results实际结果
one
0 False
1 False
2 True
3 True
2 and 4 are present in df2 , still due to order false is given. df2 中存在 2 和 4 ,仍然是由于给出了 false 的命令。 is there a way where order of values don't impact
有没有一种方法可以不影响值的顺序
You can compare columns:您可以比较列:
print(df1['one'].isin(df2['one']))
0 True
1 True
2 True
3 True
Name: one, dtype: bool
Or convert values of DataFrame to 1d array and then list:或者将 DataFrame 的值转换为一维数组,然后列出:
print(df1.isin(df2.to_numpy().ravel().tolist()))
one
0 True
1 True
2 True
3 True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.