[英]Copying dataframes columns into another dataframe
I have two dataframes df1
and df2
where df1
has 9 columns and df2
has 8 columns.我有两个数据
df1
和df2
,其中df1
有 9 列, df2
有 8 列。 I want to replace the first 8 columns of df1
with that of df2
.我想用
df2
替换df1
的前 8 列。 How can this be done?如何才能做到这一点? I tried with
iloc
but not able to succeed.我尝试使用
iloc
但未能成功。
Following are the files:以下是文件:
https://www.filehosting.org/file/details/842516/tpkA0t2vAtkrqKTb/df1.csv for df1
https://www.filehosting.org/file/details/842516/tpkA0t2vAtkrqKTb/df1.csv
df1
https://www.filehosting.org/file/details/842517/8XpizwCAX79p9rrZ/df2.csv for df2
https://www.filehosting.org/file/details/842517/8XpizwCAX79p9rrZ/df2.csv for
df2
import pandas as pd
df1=pd.DataFrame({0:[1,1,1,0,0,0],1:[0,1,0,0,0,0],2:[1,1,1,0,0,0],3:[0,0,0,2,3,4],4:[0,0,0,0,1,0],5:[0,0,0,2,1,2]})
df2=pd.DataFrame({6:[2,2,2,0,0,0],7:[0,2,0,0,0,0],8:[2,2,2,0,0,0],'d':[0,0,0,2,3,4],'e':[0,0,0,0,1,0],'f':[0,0,0,2,1,2]})
z=pd.concat([df1.iloc[:,3:],df2.iloc[:,0:3]],axis=1)
Here I have concatenated from 3rd column to last column of 1st dataframe and the first 3 column of 2nd dataframe.在这里,我将第 3 列连接到第 1 个数据帧的最后一列和第 2 个数据帧的前 3 列。 Similarly you concatenate whichever row or column you want to concatenate
同样,您连接要连接的任何行或列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.