如何根据 pandas 中的条件映射两行不同的数据框 [英] how to map two rows of different dataframe based on a condition in pandas
本文介绍了如何根据 pandas 中的条件映射两行不同的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数据框
df1,
Names
one two three
Sri is a good player
Ravi is a mentor
Kumar is a cricketer player
df2,
values
sri
NaN
sri, is
kumar,cricketer player
我正在尝试在df1中获取包含df2中所有项目的行
I am trying to get the row in df1 which contains the all the items in df2
我的预期输出是
values Names
sri Sri is a good player
NaN
sri, is Sri is a good player
kumar,cricketer player Kumar is a cricketer player
我尝试了,df1["Names"].str.contains("|".join(df2["values"].values.tolist()))
我也尝试过,
i tried, df1["Names"].str.contains("|".join(df2["values"].values.tolist()))
I also tried,
但是我无法达到预期的输出,因为它具有(,").请帮助
but I cannot achieve my expected output as it has (","). Please help
推荐答案
在Numpy广播中使用集合逻辑.
Using set logic with Numpy broadcasting.
d1 = df1['Names'].fillna('').str.lower().str.split('[^a-z]+').apply(set).values
d2 = df2['values'].fillna('').str.lower().str.split('[^a-z]+').apply(set).values
i, j = np.where(d1 >= d2[:, None])
df2.assign(Names=pd.Series(df1['Names'].values[j], df2['values'].index[i]))
values Names
0 sri Sri is a good player
1 NaN NaN
2 sri, is Sri is a good player
3 kumar,cricketer player Kumar is a cricketer player
这篇关于如何根据 pandas 中的条件映射两行不同的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文