Pirinthan
Pirinthan

Reputation: 524

How to insert columns values in pandas dataframe into the equation to find the Y values using python?

pandas Dataframe return below table . need to calculate the y values according to the following equation for each and every row and need to add to same DataFrame with new field y_value.

       Discount     Profit
0         2          100
1         1          200
2         6          100
3         6          100
4         4          140
5         2          120
6         2          80

Equation

y = Discount + 2 * Profit + 27

After calculate the new field y_value, DataFrame should be return like

     Discount     Profit  y_value
0         2       100      229
1         1       200      428
2         6       100      233
3         6       100      233
4         4       140      311
5         2       120      269
6         2       80       189

Upvotes: 0

Views: 1446

Answers (1)

boot-scootin
boot-scootin

Reputation: 12515

pandas supports this type of operation easily:

df['y_value'] = df['Discount'] + 2 * df['Profit'] + 27

df
Out[6]: 
   Discount  Profit  y_value
0         2     100      229
1         1     200      428
2         6     100      233
3         6     100      233
4         4     140      311
5         2     120      269
6         2      80      189

Upvotes: 1

Related Questions