multiply columns pandas with each other

Question:

I have a pandas dataframe and I want to add to it new features, like this:

Say I have features X_1,X_2,X_3 and X_4, then I want to add X_1 * X_2, X_1 * X_3, X_1 * X_4, and similarly X_2 * X_3, X_2 * X_4 and X_3 * X_4. I want to add them, not replace the original features.

How do I do that?

Asked By: rose

||

Answers:

You just multiply the columns and assign them to new columns:

import pandas as pd

df = pd.DataFrame({'One': [1, 2, 3, 4], 'Two': [4, 3, 2, 1]})
print(df)

df['One times two'] = df['One'] * df['Two']
print(df)

outputs

   One  Two
0    1    4
1    2    3
2    3    2
3    4    1
   One  Two  One times two
0    1    4              4
1    2    3              6
2    3    2              6
3    4    1              4
Answered By: Alex Bochkarev

Let’s say all are integers X_1,X_2,X_3 and X_4. You can create new nan columns and could add what do you want in there.

df['X_1multipleX_2'] = np.nan
df['X_1multipleX_2'] = df['X_1']*df['X_2'] #You can do it without first step.
Answered By: ali bakhtiari
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.