如何检查一个数据框中的值是否存在于另一数据框中的键中 [英] How to check if a value in one dataframe is present in keys in the other dataframe
本文介绍了如何检查一个数据框中的值是否存在于另一数据框中的键中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数据框:
df_1:
Letters Boolean
a Nan
b Nan
c Nan
df_2:
a b d
2.7 1.2 3.6
1 2 3
如何检查df_2.keys()中是否存在df_1 ['letters'].如果存在,我希望布尔值采用值"x":像这样:
How do I check if df_1['letters'] is present in df_2.keys(). If it is present, I want boolean to take the value 'x': Something like:
Letters Boolean
a x
b x
c Nan
我尝试使用此代码:
for x in df_1['letters']:
if x in df_2.keys():
df_1['Boolean']='x'
推荐答案
使用 isin
:
df1['Boolean'] = np.where(df1['Letters'].isin(df2.columns), 'x', np.nan)
这篇关于如何检查一个数据框中的值是否存在于另一数据框中的键中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文