1

I have a dataframe where i need to create new column based on the multiplication of other column with specific column

Here is how my data frame looks.

df:

Brand     Price      S_Value        S_Factor
A         10          2             2
B         20          4             1
C         30          2             1
D         40          1             2
E         50          1             1
F         10          1             1

I would like multiply column Value and Factor with Price to get new column. I can do it manually but I have a lot of column and all start with specific prefix wihivh i need to multiply... here I used S_ which mean I need to multiply all the columns which start with S_

Here would be te desired output columns

Brand     Price      S_Value        S_Factor       S_Value_New       S_Factor_New
A         10          2             2
B         20          4             1
C         30          2             1
D         40          1             2
E         50          1             1
F         10          1             1
s_khan92
  • 969
  • 8
  • 21
  • Further Reading: https://stackoverflow.com/questions/27275236/pandas-best-way-to-select-all-columns-whose-names-start-with-x , https://stackoverflow.com/questions/14059094/i-want-to-multiply-two-columns-in-a-pandas-dataframe-and-add-the-result-into-a-n – Abdel-Raouf Feb 01 '21 at 18:36

2 Answers2

2

Firstly, to get the columns which you have to multiply, you can use list comprehension and string function startswith. And then just loop over the columns and create new columns by muptiplying with Price

multiply_cols = [col for col in df.columns if col.startswith('S_')]
for col in multiply_cols:
    df[col+'_New'] = df[col] * df['Price']

df

enter image description here

ggaurav
  • 1,764
  • 1
  • 10
  • 10
1

Since you did not added and example of the output. This might be what you are looking for:

dfr = pd.DataFrame({
    'Brand' : ['A', 'B', 'C', 'D', 'E', 'F'], 
    'price'  : [10, 20, 30, 40, 50, 10],
    'S_Value'  : [2,4,2,1,1,1],
    'S_Factor'  : [2,1,1,2,1,1]
})

pre_fixes = ['S_']
for prefix in pre_fixes:
    coltocal = [col for col in dfr.columns if col.startswith(prefix)]
    for col in coltocal:
        dfr.loc[:,col+'_new'] = dfr.price*dfr[col]
dfr

    Brand   price   S_Value S_Factor    S_Value_new S_Factor_new
0   A   10  2   2   20  20
1   B   20  4   1   80  20
2   C   30  2   1   60  30
3   D   40  1   2   40  80
4   E   50  1   1   50  50
5   F   10  1   1   10  10

Just add as many prefixes you have to pre_fixes (use come to separate them)

Jorge
  • 2,181
  • 1
  • 19
  • 30