This is very closely related to Removing space from columns in pandas so I wasn't sure whether to add it to a comment to that...
the difference in my question is specifically relating to the use of a loc locator to slice out a subset...
df['py'] = df['py'].str.replace(' ','')
-- this works fine; but when I only want to apply it on the subset of rows where the column subset is 'foo':
df.loc[df['column'] == 'foo']['py'] = df.loc[df['column'] == 'foo']['py'].str.replace(' ','')
...doesn't work.
What am I doing wrong? I can always slice out the group and re-append it, but curious where I'm going wrong here.
A dataset for trials:
df = pd.DataFrame({'column':['foo','foo','bar','bar'], 'py':['a b','a b','a b','a b']})
Thanks
You want:
df.loc[df['column'] == 'foo', 'py'].apply(lambda x: x.replace(' ',''))
Note the notation of loc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With