I wish to create multiple calculated fields which are referenced to other columns, based on conditions using Pandas
Data
ID status used
AA False 3
BB True 2
Desired
ID status used new
AA False 3 0.6
BB True 2 2
Doing
df['new'] = df.apply(lambda row: row['used'] * 0.2 if row['status'] ==
'False' else row['used'] * 1, axis = 1)
Any suggestion is appreciated
The following is not calculating off of the correct field
>Solution :
With this code:
import pandas as pd
data = {
'ID': ['AA', 'BB'],
'status': [False, True],
'used': [3, 2]
}
df = pd.DataFrame(data)
df['new'] = df.apply(lambda row: row['used'] * 0.2 if row['status'] == False else row['used'] * 1, axis=1)
print(df)
I can create your desired output:
ID status used new
0 AA False 3 0.6
1 BB True 2 2.0