简体   繁体   English

将一个数据帧中的列添加到另一个 python pandas

[英]Adding column(s) from one dataframe to another python pandas

I have been searching online and found similar questions but still couldn't find the answer to what I'm looking for.我一直在网上搜索并发现了类似的问题,但仍然找不到我正在寻找的答案。 I have 2 excel files:我有2个excel文件:

data1数据1

ColumnA    columnB    columnC   columnD
  A          B          C          D
  A          B          C          D
  A          B          C          D

data2数据2

ColumnE    columnF    columnG   
  E          F          G          
  E          F          G          
  E          F          G    

I want to add the column F from data2 to data1:我想将列 F 从 data2 添加到 data1:

ColumnA    columnB    columnC   columnD  columnF 
  A          B          C          D       F
  A          B          C          D       F
  A          B          C          D       F

I tried我试过

data2['columnF'] = data1['columnF']  #doesn't work

also tried也试过

data1['columnF'] = ''   #adding a columnF to data1
merg_left = pd.merge(left=data1,right=data2, how='left',      
left_on='columnF', right_on='columnF')  
#gave me a weird output file 
import pandas as pd
import io

data = """
ColumnA    columnB    columnC   columnD
  A          B          C          D
  A          B          C          D
  A          B          C          D
    """
data1 = """
ColumnE    columnF    columnG
  E          F          G
  E          F          G
  E          F          G
    """

df = pd.read_csv(io.StringIO(data), delimiter='\s+')
df1 = pd.read_csv(io.StringIO(data1), delimiter='\s+')

df['columnF'] = pd.Series(df1['columnF'])

print(df)

Will give you:会给你:

  ColumnA columnB columnC columnD columnF
0       A       B       C       D       F
1       A       B       C       D       F
2       A       B       C       D       F
import pandas as pd
f_column = data2["columnF"]
data1 = pd.concat([data1,f_column], axis = 1)
data1
     columnA      columnB     columnC     columnF
0       a            b            c         f
1       a            b            c         f
2       a            b            c         f
import pandas as pd

f_column = data2["columnF"] 

data1 = pd.concat([data1,f_column], axis = 1) 

data1

will give the desired output. 将给出所需的输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一列除以另一列,其中一个数据帧的列值对应于 Python Pandas 中另一个数据帧的列值? - How to divide one column by another where one dataframe's column value corresponds to another dataframe's column's value in Python Pandas? Pandas(Python) - 从条件更新另一个数据框的列 - Pandas (Python) - Update column of a dataframe from another one with conditions 将特定列从Pandas Dataframe添加到另一个Pandas Dataframe - Adding A Specific Column from a Pandas Dataframe to Another Pandas Dataframe 从Pandas数据框中的某一行获取某些列值,然后将其添加到另一数据框中 - Taking certain column values from one row in a Pandas Dataframe and adding them into another dataframe 将列从一个 pandas DataFrame 映射到另一个 - Mapping column from one pandas DataFrame to another 将一列从一个 pandas dataframe 复制到另一个 - Copying a column from one pandas dataframe to another Pandas Dataframe:df 从另一个 df1 dataframe 添加列 - Pandas Dataframe: df adding a column from another df1 dataframe 从另一个具有不同索引的 dataframe 在 pandas dataframe 添加新列 - Adding a new column in pandas dataframe from another dataframe with differing indices 通过匹配变量将值从一个 pandas dataframe 添加到另一个 dataframe - Adding value from one pandas dataframe to another dataframe by matching a variable 根据 pandas python 中另一列的组从一个 DataFrame 列创建标签 - Creating a tag from one DataFrame column according to a group from another column in pandas python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM