简体   繁体   English

如何将熊猫数据帧的两列相乘(行乘法)并将结果存储在新列中?

[英]How to multiply two columns of a pandas data-frame (row multiplication) and store the result in a new column?

Command命令

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

TypeError: can't multiply sequence by non-int of type 'str'类型错误:不能将序列乘以“str”类型的非整数

The attacehd picture ( https://i.stack.imgur.com/0Asu3.png ) has more information. attacehd 图片( https://i.stack.imgur.com/0Asu3.png )有更多信息。 My code snippet is at i.stack.imgur.com/ehqF5.png)我的代码片段位于 i.stack.imgur.com/ehqF5.png)

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

First you need to convert the column type of the two columns to floats (otherwise you cannot multiply them).首先需要将两列的列类型转换为浮点数(否则不能相乘)。 You can do that as follows:你可以这样做:

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

Then you can define the new column via multiplication:然后您可以通过乘法定义新列:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM