使用panda

时间:2019-06-04 01:20:10

标签: pandas

我有两个数据框,我想将值从一个复制到另一个。将列值复制到数据框时返回NaN

这些是我的df:

data1 = [[1, 2], [3, 4], [5, 6]]
rc = pd.DataFrame(data, columns = ['Sold', 'Leads'])

data2 = [['Company1','2017-05-01',0, 0], ['Company1','2017-05-01',0, 0], ['Company1','2017-05-01',0, 0]]
final = pd.DataFrame(data2, columns = ['company','date','2019_sold', '2019_leads'])

我尝试了位置索引

final.loc[(final['date'] == '2017-05-01') & (final['company'] == 'Company1'),['2019_sold','2019_leads']] = rc[['Leads','Sold']]

我希望他们将rc df的精确值复制到最终df,但返回的值NaN

1 个答案:

答案 0 :(得分:0)

通过使用update

rc.index=final.index[(final['date'] == '2017-05-01') & (final['company'] == 'Company1')]
rc.columns=['2019_sold','2019_leads']
final.update(rc)
final
Out[165]: 
    company        date  2019_sold  2019_leads
0  Company1  2017-05-01          1           2
1  Company1  2017-05-01          3           4
2  Company1  2017-05-01          5           6