I am just starting to learn python so please ignore or let me know if I am not asking this questions properly.
I have a dataframe df
df = pd.DataFrame({'A' : [5,6,0,-4], 'B' : [1,2,3,5]})
A | B |
---|---|
5 | 1 |
6 | 2 |
0 | 3 |
-4 | 5 |
I want to do the below:
IF A greater than 0 then DO;
----IF B < 2 then NEWVALUE = A * 2
----ELSE A = A/2
IF A = 0 then CALC_FIELD = NEWVALUE + 2 # use the NEWVALUE column that is just created in prior step
I am using the below combination of 'for' and 'if' to get the output. But is there a better and efficient way to do this ?
for i in range(df.shape[0]):
if (df.loc[i, 'A'] > 0) & (df.loc[i, 'B'] < 2): df.loc[i,'NEWVALUE'] = df.loc[i,'A'] * 2
if (df.loc[i, 'A'] <= 0): df.loc[i, 'NEWVALUE'] = df.loc[i,'A'] / 2
if (df.loc[i, 'A'] == 0): df.loc[i, 'CALC_FIELD'] = df.loc[i, 'NEWVALUE'] + 2
**EXPECTED OUTPUT**
| A | B | C | D |
| -- | -- | -- |-- |
| 5 | 1 | 10 | Nan|
| 6 | 2 | Nan| Nan|
| 0 | 3 | 0 | 2 |
| -4 | 5 | -2 |Nan |