RoiMinuit
RoiMinuit

Reputation: 414

Create a column based on two other columns

I want to create a column based on the values of two other columns; here's my code:

import pandas as pd
df = pd.DataFrame({'pre_decrease_addfund':[7, 50, 0, 44, 41],
                  'decrease':[0, 43, 0, 0, 41],
                  'add_fund':[5, 0, 38, 10, 97],
                  'post_decrease_addfund':[12, 7, 38, 54, 97]})

print(df)

The table should look like this, where 'pre_decrease_addfund' is the objective column, built off of 'decrease' and 'add_fund' appropriately. i.e., notice how row 4 had a 41-dollar decrease and 97-dollar added and 'post_decrease_addfund' reflects these actions accurately.

pre_decrease_addfund  decrease  add_fund  post_dcrease_addfund
0                     7         0         5                    12
1                    50        43         0                     7
2                     0         0        38                    38
3                    44         0        10                    54
4                    41        41        97                    97

Upvotes: 0

Views: 102

Answers (1)

ombk
ombk

Reputation: 2111

df = pd.DataFrame({'pre_decrease_addfund':[7, 50, 0, 44, 41],
                  'decrease':[0, 43, 0, 0, 41],
                  'add_fund':[5, 0, 38, 10, 97]})

df["post_decrease_add"] = df.pre_decrease_addfund - df.decrease + df.add_fund

Upvotes: 3

Related Questions