Reputation: 1741
I want to check row by row whether certain conditions are met, and then assign values to multiple columns.
this does what i want,
df.ix[(df.city.isin(["UNKNOWN"]) | df.city.isnull()) & (df.street.str[0:4].str.contains('TRAN')|df.street.str[0:4].str.contains('TRAS')| \
df.street.str[0:4].str.contains('HOM')|df.street.str.contains("(HOMELESS)|")|df.city.str.contains("HOMELESSS") ),'city']="HOMELESS"
df.ix[(df.city.isin(["UNKNOWN"]) | df.city.isnull()) & (df.street.str[0:4].str.contains('TRAN')|df.street.str[0:4].str.contains('TRAS')| \
df.street.str[0:4].str.contains('HOM')|df.street.str.contains("(HOMELESS)|")|df.city.str.contains("HOMELESSS") ),'homeless']=1
but i'm wondering if there's a way to do it with out iterating twice.
Upvotes: 1
Views: 498
Reputation: 97331
you can use a list as the index of the second axis:
df = pd.DataFrame(np.random.randint(0, 10, size=(10, 4)), columns=list("ABCD"))
df.ix[(df.A > 5) & (df.B < 8), ["A", "B"]] = -10, -10
Upvotes: 4