Reputation: 11
I'm trying to run a section of script that when it read a CSV file it does what the script says.
This is the code rn:
x = input("Do you want to run a mini campaign? Type y for yes or n for no: ")
x = x.lower().strip()
if x == "y":
df['Employee_Departments__c'] = df['Department'].apply(lambda x: 'Real Estate' if x == 'Operations' else x)
df['Employee_Departments__c'] = df['Department'].apply(lambda x: 'Human Resources' if x == ['C-Suite', 'Human Resources'] else x)
df['GPEC BI'] = df['Employee_Departments__c'].apply(lambda x:'MC' if x == 'Human Resources' or x == 'Real Estate' else '')
df=df.drop(['Department'], axis=1)
else:
df=df.drop(['Department', 'Employee_Departments__c', 'GPEC BI'], axis=1) #Removes the added columns if n is typed.
pass
When I run this code it doesn't label the operations label as Real Estate, instead it leaves it as operations. But when I change C-Suite to label it is Human Resources it works. However, if I switch the Real Estate line and the Human resources line, the Real Estate line works and the HR doesn't.
I'm kinda new to python, but couldn't find how to make this work the way I need it to, when reading Python books and other suggestions.
I tried to run it and expect the column to be filled with the appropriate content that I requested. Tried doing an or, if, else, and where statement to see if that'll help, but no. Only one of the lines works and I need both to work.
Upvotes: 1
Views: 69
Reputation: 11
I think the reason only one of the two lines are working is that you are trying to do an if-elif-else to fill one new column df['Employee_Departments__c']
. The second line works because it overrides the first line. I would suggest you change the structure of lambda function following this answer:
https://stackoverflow.com/a/44991451/21088924.
Two lines would then be combined to one line:
df['Employee_Departments__c'] = df['Department'].apply(lambda x: 'Real Estate' if x == 'Operations' else ('Human Resources' if x == ['C-Suite', 'Human Resources'] else x))
Upvotes: 1