简体   繁体   English

将数据框列复制到另一个数据框

[英]Copying dataframes columns into another dataframe

I have two dataframes df1 and df2 where df1 has 9 columns and df2 has 8 columns.我有两个数据df1df2 ,其中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.

相关问题 基于python中另一个数据框的2列过滤数据框 - Filter dataframes based on 2 columns of another dataframe in python 将两列不同的数据帧连接到另一个 dataframe - join two columns of different dataframes into another dataframe 从另一个 Dataframe 的列迭代创建数据帧 - Create Dataframes iteratively from columns of another Dataframe 在DataFrame之间复制现有列 - Copying existing columns between DataFrames 使用来自另一个 dataframe 的具有特定列值的行创建一个新的数据框 - create a new dataframes with rows from another dataframe with specific columns values 在 DataFrame 中查找未被其他 DataFrames 列中的值中断的连续列值 - Finding continuous column values in a DataFrame that are not interrupted by values in another DataFrames columns 如果任何行值在另一个数据框中,则从数据框中删除一行,数据框具有多列 - remove a row from a dataframe if any row value is in another dataframe , with dataframes having multiple columns 如果第一个 dataframe 中的列的数据存在于 python 中另一个 dataframe 的任何列中,则合并两个数据帧 - Merge two dataframes if data of a column in first dataframe exists in any of the columns of another dataframe in python 通过变量复制数据框与通过列复制变量 - Copying dataframes through variables vs copying through variables for columns 选择用于复制 Pandas DataFrame 的列 - Selecting columns for copying Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM