简体   繁体   English

用另一个数据框的值填充数据框列

[英]Filling dataframe column with values of another dataframe

I have two dataframes: 我有两个数据框:

import pandas
import numpy
entry1= pandas.datetime(2014,6,1)
entry2= pandas.datetime(2014,6,2)
df1=pandas.DataFrame(numpy.array([[1,1],[2,2],[3,3]]), columns=['eins','zwei'], index=[entry1, entry1, entry2])   
df2=pandas.DataFrame(numpy.array([[2,3],[3,3]]), columns=['eins','zwei'], index=[entry1, entry2])  

and I want to insert a new column into df1 that looks up the corresponding index values in df2 and inserts the value of column 'eins' that has the same index value as df1 into the new column of df1.The result is supposed to look like this: 我想在df1中插入一个新列,以查找df2中的相应索引值,并将与df1具有相同索引值的列'eins'的值插入到df1的新列中。结果应该看起来像这个:

df1['new column']=[2,2,3]

How to do this kind of filling? 这种填充怎么做?

>>> df1.join(df2['eins'], rsuffix='_new')
            eins  zwei  eins_new
2014-06-01     1     1         2
2014-06-01     2     2         2
2014-06-02     3     3         3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM