我必须根据某些条件构建数据框的列。我通常使用np.where()函数,但是按我的预期,它不起作用。我需要根据另一列的值向df2添加一列,在我们的例子中是'Let'。当它等于“ a”时,有必要在另一个数据框中搜索一个值,特别是它必须返回df1的“ Dates”字段,其中“ Species”字段等于“ Animal”字段的值在该索引中('Let'== a)。 “年龄”和“保证”的理由相同。我的代码:
df1 = pd.DataFrame({'Species' : ['Falcon',
'Falcon','Falcon','tiger','Falcon','Dog'],
'Dates': ['30-01-2019', '30-01-2019','30-01-2018', '30-01-
2017', '30-01-2019', '30-01-2021'],
'Age' : [ 2.55, 23, 31, 2, -12.55, 31]})
df2 = pd.DataFrame({'Animal' : ['Dog',
'Falcon','Dog','tiger','Falcon','cat'],
'Mesure' : [2.55, 23, 31, 2,22,4],
'Let' : ['a', 'b', 'a', 'a', 'a','b']})
np.where(df2['Let'] != 'a', 1, df1[(df1['Species'] == df2['Animal'])&
(df1['Age'] == df2['Mesure'])]['Dates'])
输出为该列:
30-01-2019 (first record of df1)
30-01-2021 (last record)
30-01-2018 (third record)
30-01-2017 (fourth record)
答案 0 :(得分:0)
不确定这是否有帮助。
new = df2.merge(df1, how = 'left', left_on = ['Animal','Mesure'], right_on = ['Species','Age'])
new = new.drop(['Species','Age'], axis = 1)
new.loc[new['Let'] == 'b','Dates'] = np.nan
new