用 pandas 中另一个数据框的列替换一个数据框的列 [英] Replacing Columns from one dataframe with columns from another dataframe in pandas

查看:69
本文介绍了用 pandas 中另一个数据框的列替换一个数据框的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个DataFrame,我们将其称为X和Y,X的尺寸为2063 x 14,Y的尺寸为2063 x8.我想用Y替换X的第4到12列,我可以在熊猫?

I have two DataFrame, let's call it X and Y, with dimension of X being 2063 x 14 and dimension of Y being 2063 x 8. I want to replace column 4 to 12 of X with Y, can I do that in pandas?

到目前为止,我发现的解决方案是替换一列/多列中的某些值,而不是一次替换整个DataFrame.感谢任何帮助. (:

The solution I found so far are replacing certain values from a column/multiple columns, but not entire DataFrame at once. Appreciate any help. (:

推荐答案

这应该有效:

X.iloc[:, 4:12] = Y

ilocloc允许我们从数据帧的切片中进行切片并分配给切片.

iloc and loc allow us to both slice from and assign to slices of a dataframe.

#            assign Y
#                |
#               /-\
X.iloc[:, 4:12] = Y
#      ^   ^
#      |   |
# slices   |
# all rows |
#          slices columns
#          5 through 12
#          which constitute
#          the 8 columns we want
#          replace with the 8
#          columns of Y

这篇关于用 pandas 中另一个数据框的列替换一个数据框的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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