I have 2 dataframes.
我有2个数据帧。
Df1 = pd.DataFrame({'name': ['Marc', 'Jake', 'Sam', 'Brad'] Df1 =
Df2 = pd.DataFrame({'IDs': ['Jake', 'John', 'Marc', 'Tony', 'Bob']
I want to loop over every row in Df1['name'] and check if each name is somewhere in Df2['IDs']
我想循环遍历Df1 ['name']中的每一行,并检查每个名字是否在Df2 ['IDs']中的某个位置
The result should return 1 if the name is in there, 0 if it is not like so:
如果名称在那里,结果应该返回1,如果不是这样,则返回0:
Marc 1
Jake 1
Sam 0
Brad 0
Thank you.
谢谢。
4 个解决方案
#1
4
Use isin
使用isin
Df1.name.isin(Df2.IDs).astype(int)
0 1
1 1
2 0
3 0
Name: name, dtype: int32
Show result in data frame
在数据框中显示结果
Df1.assign(InDf2=Df1.name.isin(Df2.IDs).astype(int))
name InDf2
0 Marc 1
1 Jake 1
2 Sam 0
3 Brad 0
In a Series object
在一个Series对象中
pd.Series(Df1.name.isin(Df2.IDs).values.astype(int), Df1.name.values)
Marc 1
Jake 1
Sam 0
Brad 0
dtype: int32
#2
4
This is one way. Convert to set for O(1) lookup and use astype(int)
to represent Boolean values as integers.
这是一种方式。转换为设置为O(1)查找并使用astype(int)将布尔值表示为整数。
values = set(Df2['IDs'])
Df1['Match'] = Df1['name'].isin(values).astype(int)
#3
4
This should do it:
这应该这样做:
Df1 = Df1.assign(result=Df1['name'].isin(Df2['IDs']).astype(int))
#4
2
By using merge
通过使用合并
s=Df1.merge(Df2,left_on='name',right_on='IDs',how='left')
s.IDs=s.IDs.notnull().astype(int)
s
Out[68]:
name IDs
0 Marc 1
1 Jake 1
2 Sam 0
3 Brad 0
#1
4
Use isin
使用isin
Df1.name.isin(Df2.IDs).astype(int)
0 1
1 1
2 0
3 0
Name: name, dtype: int32
Show result in data frame
在数据框中显示结果
Df1.assign(InDf2=Df1.name.isin(Df2.IDs).astype(int))
name InDf2
0 Marc 1
1 Jake 1
2 Sam 0
3 Brad 0
In a Series object
在一个Series对象中
pd.Series(Df1.name.isin(Df2.IDs).values.astype(int), Df1.name.values)
Marc 1
Jake 1
Sam 0
Brad 0
dtype: int32
#2
4
This is one way. Convert to set for O(1) lookup and use astype(int)
to represent Boolean values as integers.
这是一种方式。转换为设置为O(1)查找并使用astype(int)将布尔值表示为整数。
values = set(Df2['IDs'])
Df1['Match'] = Df1['name'].isin(values).astype(int)
#3
4
This should do it:
这应该这样做:
Df1 = Df1.assign(result=Df1['name'].isin(Df2['IDs']).astype(int))
#4
2
By using merge
通过使用合并
s=Df1.merge(Df2,left_on='name',right_on='IDs',how='left')
s.IDs=s.IDs.notnull().astype(int)
s
Out[68]:
name IDs
0 Marc 1
1 Jake 1
2 Sam 0
3 Brad 0