I am trying to calculate the final revenue of my dataset. My dataset has several revenue streams, but given some conditions (that I will explain later) the revenue per client will be calculated differently for the final revenue.
I am not very comfortable creating functions yet so I'm not sure where I am making mistakes.
Dataframe examples:
ClientId Sector Class Rev1 Rev2 Rev3
1 Sect_1 B 5 1 0
2 Sect_2 A 5.5 2 0
3 Sect_3 B 6 1.5 1
4 Sect_4 A 5 1 1.5
5 Sect_5 B 5 2 1
I want to create a 7th column 'Final_Rev' given the following conditions:
- If 'Sector' = (Sect_3 or Sect_4) : 'Final_Rev' = Rev2 + Rev3
- OR if 'Class' = ("A") : 'Final_Rev' = Rev2 + Rev3
- Otherwise 'Final_Rev' = Rev1
Expected Output should be the following:
ClientId Sector Class Rev1 Rev2 Rev3 Final_Rev
1 Sect_1 B 5 1 0 5
2 Sect_2 A 5.5 2 0 2
3 Sect_3 B 6 1.5 1 2.5
4 Sect_4 A 5 1 1.5 2.5
5 Sect_5 B 5 2 1 5
I have tried to create the following function but I'm not sure what I'm doing wrong:
def Final_Rev():
if Sector in ['Sect_3','Sect_4'] or Class == 'A':
return df['Rev2'] + df['Rev3']
else:
return df['Rev1']
df['Final_Rev'] = df.apply(Final_Rev, axis=1)
I have found an R solution that does what I want but I don't know how to convert it to python:
Final_Rev := ifelse(test = (Sector %in% c("Sect_3","Sect_4")|Class == "A"),
yes = Rev2 + Rev3,
no = Rev1
If someone could help me solve this, it would be really appreciate. Thanks.
Aucun commentaire:
Enregistrer un commentaire