简体   繁体   中英

Pandas combine 2 Dataframes and overwrite values

I've looked into pandas join, merge, concat with different param values (how to join, indexing, axis=1, etc) but nothing solves it!

I have two dataframes:

x = pd.DataFrame(np.random.randn(4,4))
y = pd.DataFrame(np.random.randn(4,4),columns=list(range(2,6)))


x
Out[67]: 
          0         1         2         3
0 -0.036327 -0.594224  0.469633 -0.649221
1  1.891510  0.164184 -0.010760 -0.848515
2 -0.383299  1.416787  0.719434  0.025509
3  0.097420 -0.868072 -0.591106 -0.672628

y
Out[68]: 
          2         3         4         5
0 -0.328402 -0.001436 -1.339613 -0.721508
1  0.408685  1.986148  0.176883  0.146694
2 -0.638341  0.018629 -0.319985 -1.832628
3  0.125003  1.134909  0.500017  0.319324

I'd like to combine to one dataframe where the values from y in columns 2 and 3 overwrite those of x and then columns 4 and 5 are inserted on the end:

new
Out[100]: 
          0         1         2         3         4         5
0 -0.036327 -0.594224 -0.328402 -0.001436 -1.339613 -0.721508
1  1.891510  0.164184  0.408685  1.986148  0.176883  0.146694
2 -0.383299  1.416787 -0.638341  0.018629 -0.319985 -1.832628
3  0.097420 -0.868072  0.125003  1.134909  0.500017  0.319324

您可以尝试combine_first

df = y.combine_first(x)

You need update and combine_first

x.update(y)
x.combine_first(y)
Out[1417]: 
          0         1         2         3         4         5
0 -1.075266  1.044069 -0.423888  0.247130  0.008867  2.058995
1  0.122782 -0.444159  1.528181  0.595939  0.155170  1.693578
2 -0.825819  0.395140 -0.171900 -0.161182 -2.016067  0.223774
3 -0.009081 -0.148430 -0.028605  0.092074  1.355105 -0.003027

Or you using pd.concat + intersection

pd.concat([x.drop(x.columns.intersection(y.columns),1),y],1)
Out[1432]: 
          0         1         2         3         4         5
0 -1.075266  1.044069 -0.423888  0.247130  0.008867  2.058995
1  0.122782 -0.444159  1.528181  0.595939  0.155170  1.693578
2 -0.825819  0.395140 -0.171900 -0.161182 -2.016067  0.223774
3 -0.009081 -0.148430 -0.028605  0.092074  1.355105 -0.003027

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