简体   繁体   中英

Replace cell values in dataframe1 with previously determined values in dataframe2

I have a dataframe like. data1 = ID ABCDO T1 1 3 3 2 1 T2 2 4 2 3 0 T3 4 1 1 4 0 T4 5 2 4 5 0 T5 3 5 5 1 1

And other data is.

data2 =

bin ABCD 1 0.2 0.1 0.3 0.4 2 0.1 0.3 0.7 0.9 3 0.5 0.7 0.8 1.0 4 0.15 0.25 0.91 0.71 5 0.35 0.05 0.71 0.58

I want to replace the columns A,B,C,D values in data1 with the bin values in data2, so that the final data looks like below.

data1_updated =

ID ABCDO T1 0.2 0.7 0.8 0.9 1 T2 0.1 0.25 0.7 1.0 0 T3 0.15 0.1 0.3 0.71 0 T4 0.35 0.2 0.91 0.58 0 T5 0.5 0.5 0.71 0.4 1

How to do this??

You can do column update like this:

>>> data1[['A', 'B', 'C', 'D']] = data2[['A', 'B', 'C', 'D']]
>>> data1
   ID     A     B     C     D  O
0  T1  0.20  0.10  0.30  0.40  1
1  T2  0.10  0.30  0.70  0.90  0
2  T3  0.50  0.70  0.80  1.00  0
3  T4  0.15  0.25  0.91  0.71  0
4  T5  0.35  0.05  0.71  0.58  1

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.

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