简体   繁体   English

如何将一个数据帧的列值附加到另一个数据帧的列

[英]How to append column values of one dataframe to column of another dataframe

I'm working with 2 dataframes, A & B. Dataframe A is populated with values, while dataframe B is empty except for a header structure I want to take the value of column in dataframe A, and append them to the corresponding column in dataframe B. 我正在处理2个数据帧,A和B.数据帧A填充了值,而数据帧B是空的,除了标题结构我想要获取数据帧A中的列值,并将它们附加到数据帧中的相应列B.

I've placed the values of the dataframe A column I want to append in a list. 我已将要追加的数据框A列的值放在列表中。 I 've tried setting the destination column values to equal the list of start column values, but that gives me the following error: 我已经尝试将目标列值设置为等于起始列值列表,但这会给我以下错误:

dataframeB[x] = list(dataframeA[A])

This yields the following error: 这会产生以下错误:

ValueError: Length of values does not match length of index

The result I expect is Dataframe A's column A transfers over to Dataframe B's column x 我期望的结果是Dataframe A的列A转移到Dataframe B的列x

  A  B  C  D  
  1  2  3  4  
  1  2  3  4  

Dataframe B 数据帧B.

  x  y 
  -  -  

Create the dataframe with the data already in it... 使用其中已有的数据创建数据框...

dataframeB = pd.DataFrame(dataframeA['A'], columns = ['x'])

Then you can add columns in from the other dataframe: 然后,您可以从其他数据框添加列:

dataframeB['y'] = dataframeA['B']

Result: 结果:

  x  y
  1  2 
  1  2

暂无
暂无

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

相关问题 如何将 append 一个 dataframe 变成另一个 dataframe 作为一列 - How to append one dataframe into another dataframe as a column pandas数据框根据另一数据框中的值将值追加到一列 - pandas dataframe append values to one column based on the values in another dataframe 如何从一个数据框中的列中提取特定值并将它们附加到另一个数据框中的列中? - 熊猫 - How do you extract specific values from a column in one dataframe and append them to a column in another dataframe? - Pandas 如何将 append 列转换为 DataFrame 以收集 Python 中另一个 DataFrame 的值? - How to append a column to a DataFrame that collects values of another DataFrame in Python? 如何检查一个数据帧中的列值是否可用或不检查另一数据帧的列中的值? - How to check values of column in one dataframe available or not in column of another dataframe? 比较另一列 dataframe 中一列的值 dataframe - Compare values of one column of dataframe in another dataframe 熊猫将数据框附加到另一个不合并列值 - Pandas append dataframe to another not merging column values 如果索引值相同,如何将一个DataFrame列复制到另一个Dataframe中 - How to copy one DataFrame column in to another Dataframe if their indexes values are the same 根据另一个 dataframe 的列值打印一个 dataframe 的列值 - print column values of one dataframe based on the column values of another dataframe append 列的值基于 dataframe 中另一列的值 - append column with values based on values of another column in the dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM