Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing spaces from a column in pandas

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

like image 727
BAC83 Avatar asked Oct 25 '25 08:10

BAC83


1 Answers

You want:

df.loc[df['column'] == 'foo', 'py'].apply(lambda x: x.replace(' ',''))

Note the notation of loc.

like image 54
vtasca Avatar answered Oct 27 '25 23:10

vtasca