如何将pandas数据帧的两列相乘(行相乘)并将结果存储到新列中?

2024-06-17 15:20:53 发布

您现在位置:Python中文网/ 问答频道 /正文

命令

df_main['Ranking'] = df_main['Porosity']*['Permeability']给出-

TypeError: can't multiply sequence by non-int of type 'str'

附件hd图片(https://i.stack.imgur.com/0Asu3.png)有更多信息。我的代码片段位于i.stack.imgur.com/ehqF5.png)

更多信息:https://i.stack.imgur.com/0Asu3.png


Tags: https命令com信息dfpngstackmain
1条回答
网友
1楼 · 发布于 2024-06-17 15:20:53

首先,需要将两列的列类型转换为浮点(否则不能相乘)。你可以这样做:

df_main[['Porosity', 'Permeability']] = df_main[['Porosity', 'Permeability']].astype(float)

然后可以通过乘法定义新列:

df_main['Ranking'] = df_main['Porosity']*df_main['Permeability']

相关问题 更多 >