1

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?

LiAfe
  • 95
  • 6
  • 1
    Use `np.select` – mozway Feb 08 '23 at 03:40
  • 1
    @mozway thanks for prompt, here is the answer to my question, `df['new'] = np.select([(df['col_5'] == 1) & (df['col_2'] != 0), (df['col_5'] == 2) & (df['col_3'] != 0), (df['col_5'] == 3) & (df['col_4'] != 0)], [df['col_2'], df['col_3'], df['col_4']], default=(df['col_1'] * np.array([0.25, 0.5, 0.75])[df['col_5']-1]))` – LiAfe Feb 08 '23 at 04:22

0 Answers0