[英]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.