python - 如何对两个不同数据帧的两列求和?

标签 python pandas

我有两个数据帧tmpdf

我想做以下求和

df.iloc[:,2] = tmp.iloc[:,2] + df.iloc[:,2]

tmp.iloc[:,2]

0         NaN
1         NaN
2         NaN
3      1693.0
4      3511.0
5         NaN
6         NaN
7      2285.0
8     63269.0
9         NaN
10    16637.0
11     4319.0
12     5361.0
13    32338.0
14    61578.0
15     9022.0
16     5648.0
17      190.0
18        NaN
19     3840.0
20    13217.0
21     8525.0
22     3725.0
23      552.0
24        NaN
25        NaN
26     3914.0
27     2328.0
28     1980.0
29    15436.0
30     5284.0
31      258.0
Name: (HOMEWORK, GAID), dtype: float64


df.iloc[:,2]

0          NaN
1          NaN
2          NaN
3       3403.0
4       6893.0
5          NaN
6          NaN
7       4605.0
8     126664.0
9          NaN
10     33022.0
11      8700.0
12     10796.0
13     64832.0
14    123594.0
15     18109.0
16     11377.0
17       394.0
18         NaN
19      7656.0
20     26441.0
21     16935.0
22      7572.0
23      1032.0
24         NaN
25         NaN
26      7824.0
27      4659.0
28      3884.0
29     30860.0
30     10562.0
31       510.0
Name: (HOMEWORK, GAID), dtype: float64

但如果尝试

df.iloc[:,2] = tmp.iloc[:,2] + df.iloc[:,2]

我得到以下信息:

TypeError: Cannot convert bool to numpy.ndarray

最佳答案

sum = df['tmp'] + df['df']  # a Series
# and
df['df'] = df['tmp'] + df['df']  # assigned to a column

关于python - 如何对两个不同数据帧的两列求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60169404/

相关文章:

python - Pandas : Compare the columns of a data frame and add a new column & value based on a condition

python - 数据框中列表的 Pandas 平均值

python - 我还可以做些什么来解决在virtualenv中没有导入到python但尚未导入到python中的软件包?

python - OpenCV 无法正确检测眼睛

python - Python中通过一列的特定总和值选择前N行

python - 如何计算 pandas 数据框中列中 True 和 False block 的数量

python - 如何使用 PyFlakes 使 Vim 错误列表永久化?

python - Pandas:自定义类作为具有多索引的列标题

python - 追加 CSV 文件,匹配无序列

python - 使用 pandas 计算时间序列中每个月的平均值