Python 熊猫将一帧中的值指定给选定行中另一帧中的相应列
我想将行7,12 inDF1设置为DF2的值,而不显式指定列Python 熊猫将一帧中的值指定给选定行中另一帧中的相应列,python,pandas,Python,Pandas,我想将行7,12 inDF1设置为DF2的值,而不显式指定列 类似于DF1[7,12]=DF2的东西,您需要使用update DF1 |col1 | col2 | col3 | 12 v1 v12 3 v3 v13 5 v5 v14 7 v10 v15 DF2 |col2| col3| x1 y2 x2 y1 output DF1 |col1 | col2 | col3 | 1
类似于DF1[7,12]=DF2的东西,您需要使用
update
DF1
|col1 | col2 | col3 |
12 v1 v12
3 v3 v13
5 v5 v14
7 v10 v15
DF2
|col2| col3|
x1 y2
x2 y1
output DF1
|col1 | col2 | col3 |
12 x2 y1
3 v3 v13
5 v5 v14
7 x1 y2
您需要使用
更新
DF1
|col1 | col2 | col3 |
12 v1 v12
3 v3 v13
5 v5 v14
7 v10 v15
DF2
|col2| col3|
x1 y2
x2 y1
output DF1
|col1 | col2 | col3 |
12 x2 y1
3 v3 v13
5 v5 v14
7 x1 y2
仅使用.loc
语句:
df2.index=[0,3]# change the index you want to update in df1, then just using update
df1.update(df2)
df1
Out[404]:
col1 col2 col3
0 12 x1 y2
1 3 v3 v13
2 5 v5 v14
3 7 x2 y1
返回:
DF1.loc[DF1.col1.isin([12,7]), 'col2'] = DF2.col2.values
仅使用.loc
语句:
df2.index=[0,3]# change the index you want to update in df1, then just using update
df1.update(df2)
df1
Out[404]:
col1 col2 col3
0 12 x1 y2
1 3 v3 v13
2 5 v5 v14
3 7 x2 y1
返回:
DF1.loc[DF1.col1.isin([12,7]), 'col2'] = DF2.col2.values