I have two data frames consisting multiple identical rows and similar columns as follows:
mydf1:
id, f1, f2 ,f3 , ..., fn
x1, 34, 45 ,32 , ..., 0
x1, 24, 55 ,1 , ..., 0
x1, 67, 43 ,5 , ..., 0
x2, 20, 89 ,4 , ..., 1
x2, 24, 50 ,1 , ..., 1
x3, 14, 15 ,1 , ..., 1
x3, 44, 25 ,11 , ..., 1
.. .. .. .. ... ..
mydaf2:
id, v1, v2 ,v3 , ...,vm
x1, 74, 4 ,32 , ..., 32
x2, 64, 5 ,10 , ..., 56
x3, 4, 3 ,50 , ..., 78
Now, I want to create a new mydf_new consisting mydf2 and one extra column 'fn' taking from mydf1. As it can be seen from mydf1, fn is a feature that contains only 1 and 0 and it is identical for every id. For example, for x1, fn always is 0, for x2, fn always is 1 and so on. I want to add those 0 and 1 in the new data frame for the corresponding id, so my desired new data frame should be like this:
mydaf_new:
id, v1, v2, v3 , ..., vm, fn
x1, 74, 4 , 32 , ..., 32, 0
x2, 64, 5 , 10 , ..., 56, 1
x3, 4, 3 , 50 , ..., 78, 1
Any idea that I fix this problem?
Try this:
df = mydf1[['id', 'fn']]
mydf_new = mydf2.merge(df, how='left', on='id')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.