Czar.Wolfgang
Czar.Wolfgang

Reputation: 77

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 -

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

The attacehd picture (https://i.sstatic.net/0Asu3.png) has more information. My code snippet is at https://i.sstatic.net/ehqF5.png)

More info: https://i.sstatic.net/0Asu3.png

Upvotes: 1

Views: 14892

Answers (1)

Miriam Farber
Miriam Farber

Reputation: 19634

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']

Upvotes: 7

Related Questions