尝试基于切片另一个数据帧在数据框中创建列

时间:2017-12-19 05:27:09

标签: python pandas select

在我的玩具示例中,我有一个原始数据框df1。我想根据df2

中的值在新数据框df1中创建一个列

如果我在SQL中这样做,那就像是,

Update df2.value = (
   select df1.value where df1.settlement_date = df2.index 
        AND 
   df1.contract_date = df2.contract date
)

我尝试通过切片来做到这一点,但它会抛出ValueError: Lengths must match to compare

做这样的程序的干净,pythonic / panda-ic方法是什么?

df1 = pd.DataFrame([[1,'2016-01-01','2016-06-01'], 
                    [2,'2016-01-01','2016-07-01'],
                    [2,'2016-01-01','2016-08-01'],
                    [2,'2016-01-02','2016-06-01'],
                    [2,'2016-01-02','2016-07-01'],
                    [2,'2016-01-02','2016-08-01'],
                    [2,'2016-01-03','2016-06-01'],
                    [2,'2016-01-03','2016-07-01'],
                    [2,'2016-01-03','2016-08-01'],
                    [2,'2016-01-04','2016-06-01'],
                    [3,'2016-01-04','2016-07-01'],
                    [4,'2016-01-04','2016-08-01']],
                   columns=['value', 'settlement_date', 'contract_date'])

df1['settlement_date'] = df1['settlement_date'].astype('datetime64')
df1['contract_date'] = df1['contract_date'].astype('datetime64')

df2 =pd.DataFrame([['2016-01-01','2016-06-01'], 
                   ['2016-01-02','2016-06-01'],
                   ['2016-01-03','2016-06-01'],
                   ['2016-01-04','2016-06-01']],
                   columns=['settlement_date', 'contract_date'])
df2['settlement_date'] = df2['settlement_date'].astype('datetime64')
df2['contract_date'] = df2['contract_date'].astype('datetime64')
df2.set_index('settlement_date', inplace=True)


df2['value']= df1[(df1['settlement_date']==df2.index) & (df1['contract_date']==df2['contract_date'])]['value']

1 个答案:

答案 0 :(得分:2)

您可以像这样执行joinmerge

df2 = df2.merge(df1,how="left",on=["settlement_date","contract_date"])

在上述情况下,请勿使用set_index,或者如果您想使用索引,则可以使用join代替。