I have the DataFrame:
import numpy as np
import pandas as pd
a = [{'name': 'A', 'col_1': 3, 'col_2': 2, 'col_3': 0.5, 'col_4': 0.2, 'col_5': 1},
{'name': 'A', 'col_1': 1, 'col_2': 0, 'col_3': 0.5, 'col_4': 0.2, 'col_5': 1},
{'name': 'B', 'col_1': 3, 'col_2': 2, 'col_3': 2, 'col_4': 0.2, 'col_5': 2},
{'name': 'B', 'col_1': 1, 'col_2': 0, 'col_3': 0, 'col_4': 0.2, 'col_5': 2},
{'name': 'C', 'col_1': 3, 'col_2': 2, 'col_3': 0.5, 'col_4': 2, 'col_5': 3},
{'name': 'C', 'col_1': 1, 'col_2': 2, 'col_3': 0.5, 'col_4': 0, 'col_5': 3}]
df = pd.DataFrame(a)
I am trying to calculate data and insert into a new column df['new']
using the following logic
df['new'] = np.where((df['col_5'] == 1) & (df['col_2'] != 0), df['col_2'], df['col_1'] * 0.25)
df['new'] = np.where((df['col_5'] == 2) & (df['col_3'] != 0), df['col_3'], df['col_1'] * 0.5)
df['new'] = np.where((df['col_5'] == 3) & (df['col_4'] != 0), df['col_4'], df['col_1'] * 0.75)
but how to merge these three entries into one line of code?