I have the following problem, and I'm unable to find the right solution.
I have a dataframe.
I need to pass the first_name to another dataframe, only if the following conditions are met:
condition #1 --> If 'id' is 1, then pass 'first_name' ONLY IF 'country' = US AND 'code' = 1 AND 'zip' = 3
condition #2 --> If 'id' is 2, then pass 'first_name' IF 'country' = US. (No need to check for code and zip. Pass first_name irrespective of code and zip)
So, in this dataframe, as per the conditions stated above, it needs to pass only - 'peter', 'mike' and 'jenny'
My code looks like:
filter1 = df['id']=='1'
filter2 = df['country'] ==1
filter3 = df['code']=='1'
filter4 = df['zip'] =='3'
#filtering data
df.where(filter1 & filter2 & filter3 & filter4, inplace = True)
#then pass first_name
new_df['first_name'] = df['first_name']
But by doing this I'm only able to apply either condition (1) or (2).
Please help. Thank you!