In Stata, I can perform a conditional replace using the following code:
replace target_var = new_value if condition_var1 == x & condition_var2 == y
What's the most pythonic way to reproduce the above on a pandas dataframe? Bonus points if I can throw the new values, and conditions into a dictionary to loop over.
To add a bit more context, I'm trying to clean some geographic data, so I'll have a lot of lines like
replace county_name = new_name_1 if district == X_1 and city == Y_1
....
replace county_name = new_name_N if district == X_N and city == Y_N
What I've found so far:
pd.replace
which lets me do stuff like the following, but doesn't seem to accept logical conditions:
`
replacements = { 1: 'Male', 2: 'Female', 0: 'Not Recorded' }
df['sex'].replace(replacements, inplace=True)
`