df1
看起来像这样:
attribute_1 attribute_2
0 A Y
1 A Z
2 B Y
3 B Z
df1 = pd.DataFrame({'attribute_1': ['A', 'A', 'B', 'B'],
'attribute_2': ['Y', 'Z', 'Y', 'Z']})
和 df2
更大,具有多行相同的属性值,也有许多与 df1
不同的列:
attribute_1 attribute_2 fruit
0 A Y apple
1 A Y banana
2 A Z melon
3 B Z orange
4 B Z grape
5 B Y pear
6 B Z orange
df2 = pd.DataFrame({'attribute_1': ['A', 'A', 'A', 'B', 'B', 'B', 'B'],
'attribute_2': ['Y', 'Y', 'Z', 'Z', 'Z', 'Y', 'Z'],
'fruit': ['apple', 'banana', 'melon', 'orange', 'grape', 'pear', 'orange']})
我想向 df1
添加一列,以检查 df2.fruit
中相应属性的值是否在 ['apple', 'orange']
,创建 desired_df
:
attribute_1 attribute_2 has_apple_or_orange
0 A Y True
1 A Z False
2 B Y False
3 B Z True
desired_df = pd.DataFrame({'attribute_1': ['A', 'A', 'B', 'B'],
'attribute_2': ['Y', 'Z', 'Y', 'Z'],
'has_apple_or_orange': [True, False, False, True]})
我该怎么做?与merge不知何故?
不确定如何描述这个问题,如果这个问题已经在其他地方得到回答,请原谅我。
最佳答案
首先比较 Series.isin
的值到新列DataFrame.assign
,然后按 GroupBy.any
聚合并通过 DataFrame.join
将新列添加到第二个 DataFrame
:
f = ['apple', 'orange']
s = (df2.assign(has_apple_or_orange = df2['fruit'].isin(f))
.groupby(['attribute_1','attribute_2'])['has_apple_or_orange']
.any())
print (s)
attribute_1 attribute_2
A Y True
Z False
B Y False
Z True
Name: has_apple_or_orange, dtype: bool
df = df1.join(s, on=['attribute_1','attribute_2'])
print (df)
attribute_1 attribute_2 has_apple_or_orange
0 A Y True
1 A Z False
2 B Y False
3 B Z True
关于python - 如果列表中包含相应属性的另一个数据帧的值,如何添加列检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62189162/