python - Pandas 按行添加另一列

我有一个数据框df

A   B   C
0.1 0.3 0.5
0.2 0.4 0.6
0.3 0.5 0.7
0.4 0.6 0.8
0.5 0.7 0.9

对于每一行,我想为数据帧 df1

中的每个元素添加一个值
X  
0.1
0.2
0.3
0.4
0.5

最终的结果是

 A   B   C
 0.2 0.4 0.6
 0.4 0.6 0.8
 0.6 0.8 1.0
 0.8 1.0 1.2
 1.0 1.2 1.4

我尝试使用 df_new =df.sum(df1, axis=0),但出现以下错误 TypeError: stat_func() got multiple values for argument 'axis' 我也愿意接受 numpy 解决方案

最佳答案

你可以使用np.add:

df = np.add(df, df1.to_numpy())
print(df)

打印:

     A    B    C
0  0.2  0.4  0.6
1  0.4  0.6  0.8
2  0.6  0.8  1.0
3  0.8  1.0  1.2
4  1.0  1.2  1.4

关于python - Pandas 按行添加另一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67351675/

相关文章:

git - 无法访问 'https://github.com/Homebrew/brew/' : L

ruby - "+1"给我的结果与 ruby​​ 中的 "+ 1"不同?

html - 列表类型消失

android - 身份验证失败 : com. microsoft.identity.client.

ios - matchedGeometryEffect 动画效果不流畅

typescript - typescript 上的 Socket.io 附加属性不存在

reactjs - 属性 'type' 在类型中缺失。但在类型 'Props' 中是必需的

javascript - 获取随机用户 API

r - 我可以按列名从多列中提取值吗?

c++ - 如何在 cpp 中为 std::unordered_map 编写自定义 hash_