Python,Pandas:将一个DF中的值替换为另一DF中的相同索引值 [英] Python, pandas: replacing values in one DF by same-index values from another DF

查看:442
本文介绍了Python,Pandas:将一个DF中的值替换为另一DF中的相同索引值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个索引完全相同的数据帧:

I have two data frames with exactly the same index:

the first one:
           0         1         2
2   0.011765  0.490196  0.015686
2   0.011765  0.490196  0.015686
2   0.007843  0.494118  0.007843
2   0.007843  0.494118  0.007843
2   0.007843  0.501961  0.011765
..       ...       ...       ...

0   0.000000  0.031373  0.039216
0   0.031373  0.082353  0.105882
0   0.094118  0.149020  0.192157
0   0.094118  0.156863  0.215686

[337962 rows x 3 columns]

和第二个:

          0         1         2
0  0.055852  0.118138  0.052386
1  0.453661  0.665857  0.441551
2  0.096394  0.635641  0.068524
3  0.952545  0.827438  0.047632
4  0.787729  0.823494  0.795792
5  0.050284  0.549379  0.592593
6  0.608805  0.215458  0.068293
7  0.775640  0.091352  0.689224

第一个DF非常庞大.我需要尽快用小DF中具有相同索引的值替换大DF中的值. 如何? 感谢您的帮助.

The first DF is quite huge. I need to replace values in huge DF by values with same index in small DF as quickly as possible. How? Thanks for any help.

推荐答案

使用第二个数据帧的索引来切片第一个数据帧,然后进行分配.

Use the index of the second dataframe to slice the first one and then assign.

df1.loc[df2.index] = df2

这篇关于Python,Pandas:将一个DF中的值替换为另一DF中的相同索引值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆