Home > Back-end >  Python create new column conditinal on values of others and usinf those values
Python create new column conditinal on values of others and usinf those values

Time:10-14

I have a problem where I want to create a new column based on values of other columns and then take the value of another column.

df:

    Type       Set     Count
1    A          Z        5
2    B          Z        9   
3    B          X        8
4    C          Y        2

I found a similar solution where the new column values (colour) are assigned in the code

df = pd.DataFrame({'Type':list('ABBC'), 'Set':list('ZZXY')})
conditions = [
    (df['Set'] == 'Z') & (df['Type'] == 'A'),
    (df['Set'] == 'Z') & (df['Type'] == 'B'),
    (df['Type'] == 'B')]
choices = ['yellow', 'blue', 'purple']
df['color'] = np.select(conditions, choices, default='black')
print(df)

But instead of a colour column i want the new column to take the value of the Count column based on these rules A&Z=Count value, B&Z=Count value, everything else =0; result looking like this:

    Type       Set     Count    New
1    A          Z        5       5
2    B          Z        9       9
3    B          X        8       0
4    C          Y        2       0

Can somone help with the code to replace the colour part with taking the value of another column?

Thank you.

CodePudding user response:

I suggest to change also the conditions to get something more friendly. You can do it like this:

df = pd.DataFrame({
    'Type': ['A', 'B', 'B', 'C'],
    'Set': ['Z', 'Z', 'X', 'Y'],
    'Count': [5, 9, 8, 2]
})
conditions = ((df['Type'].isin(['A', 'B'])) & (df['Set'] == 'Z'))
df['New'] = 0  # or df.loc[~conditions, 'New'] = 0
df.loc[conditions, 'New'] = df['Count']
    Type       Set     Count    New
1    A          Z        5       5
2    B          Z        9       9
3    B          X        8       0
4    C          Y        2       0

CodePudding user response:

Similar to @ErnestBidouille, you can copy the values of the Count column and use negation to replace the row with 0.

df['New'] = df.Count
condition = ~(df.Type.isin(['A','B']) & df.Set.isin(['Z']))
df.loc[condition, 'new'] = 0
  • Related