1

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.

2
  • 'df['Employee_Departments__c'] = df['Department'].apply(lambda x: 'Human Resources' if x == ['C-Suite', 'Human Resources'] else x)' Commented Jan 26, 2023 at 23:09
  • I dont think it should be x == ['C-Suite', 'Human Resources'] Commented Jan 26, 2023 at 23:11

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.