pandas 将列的值添加到不同的数据框 [英] Pandas Add Values of Column to Different Dataframe
问题描述
所以我有一个DataFrame df1,它有3列,分别是A,B和C:
So I have a DataFrame, df1, that has 3 columns, A, B, and C as such:
A B C
Arizona 0 2.800000 5.600000
California 0 18.300000 36.600000
Colorado 0 2.666667 5.333333
Connecticut 0 0.933333 1.866667
Delaware 0 0.100000 0.200000
Florida 0 0.833333 1.666667
Georgia 0 0.000000 0.000000
Hawaii 0 1.000000 2.000000
Illinois 0 3.366667 6.733333
Indiana 0 0.000000 0.000000
Iowa 0 0.000000 0.000000
然后我有了另一个只有一列D的数据框df2:
I then have another dataframe, df2, that has just one column, D:
D
Arizona 13
California 18
Colorado 5
Connecticut 15
Delaware 7
Florida 5
Georgia 13
Hawaii 3
Illinois 21
Indiana 2
Iowa 4
我想做的是将D列的值添加到df1中的所有列.通过添加,我的意思是将[Arizona,A]的值添加到[Arizona,D]的值,而不将列D添加为新列.到目前为止,我尝试使用
What I'd like to do is add the values of column D to all the columns in df1. By add I mean take the value of [Arizona, A] and add it to the value of [Arizona, D] not add column D as a new column. So far I tried using
df1 + df2 #returned all NaN
df1 + df2['D'] #Also returned all NaN
df1['A'] + df2['D'] #Returned a new dataframe with each as a separate column
我现在不完全确定从这儿去哪里,所以我希望获得一些有关如何解决此问题的建议.似乎并不难,而且我可能缺少明显的东西.任何帮助将不胜感激.
I'm now not entirely sure where to go from here so I'd love some advice on how to solve this. It doesn't seem like it should be difficult and I'm probably missing something obvious. Any help would be appreciated.
推荐答案
,您可以使用 查看全文