Multiplying large number of pandas column to each other
I have a dataframe df
as follows:
Col1 Col2 Col3
1 4 6
2 5 7
3 6 8
I want to get the following:
Col1 Col2 Col3 Mult
1 4 6 24
2 5 7 70
3 6 8 144
where df['Mult'] = df['Col1']*df['Col2']*df['Col3']
Except in my case I have about 50 columns Col1, Col2, ..Col50.
So, using the following method is going to be very tedious df['col1'].multiply(df['Col2'], axis=1)
Is there a faster way? I am trying to avoid using the apply
method for each row as I think that will be very slow as my dataframe has about 1 million rows.
Use prod
:
df['Mult'] = df.prod(1)
Or if a subset of columns:
df['Mult'] = df[['Col1', 'Col2', 'Col3']].prod(1)
Output:
Col1 Col2 Col3 Mult
0 1 4 6 24
1 2 5 7 70
2 3 6 8 144
import pandas as pd
df = pd.DataFrame({"Col1":[1,2,3],
"Col2":[4,5,6],
"Col3":[6,7,8]})
df["Mult"] = df.prod(1)
Output:
df
Col1 Col2 Col3 Mult
0 1 4 6 24
1 2 5 7 70
2 3 6 8 144
You can also use np.prod
:
df['Mult'] = np.prod(df, axis=1)