4

I think its simple question but i have problem with it.

How can I delete spaces in multiple columns at once in pandas dataframe? example df:

A, B, C, D, E
 d,d ,s,s,a 
a ,a ,s,a ,r

I want to remove space in columns :A,B,D,E

normally i am using this method :

df['col']=df['col'].apply(lambda x: x.strip())

extending this one above i tried to use :

df[['A','B','D','E']]=df[['A','B','D','E']].apply(lambda x : x.strip())

but I am receiving an error: AttributeError: ("'Series' object has no attribute 'strip'"

how to solve it?

additionally Removing space from dataframe columns in pandas this one is not a duplicate, its only for columns names

sygneto
  • 1,761
  • 1
  • 13
  • 26

2 Answers2

4

Use Series.str.strip, because working with Series (columns):

print (df)
    A   B  C   D   E
0   d  d   s   s  a 
1  a   a   s  a    r

df[['A','B','D','E']]=df[['A','B','D','E']].apply(lambda x : x.str.strip())
print (df)
   A  B  C  D  E
0  d  d  s  s  a
1  a  a  s  a  r

Your solution should be possible with DataFrame.applymap for element wise processing:

df[['A','B','D','E']]=df[['A','B','D','E']].applymap(lambda x : x.strip())

Or use if possible:

df = pd.read_csv(file, skipinitialspace=True)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • there is no error anymore, but spaces has not been removed, also tried applymap, but spaces still there, any idea why? – sygneto Nov 08 '19 at 07:28
  • @sygneto - hmm, for me working nice, maybe some data related problem, is possible check how data looks in list? `print (df['A'].tolist())` ? – jezrael Nov 08 '19 at 07:32
  • 1
    thanks, i have found a problem in other column I had something wrong in my real data, it was like A_B, and in other rows A__B, and because of that this row with two '_' moved my other columns, thats why i thought i should delete space. anyway answer is ok for my question – sygneto Nov 08 '19 at 07:39
1

Other way is using the column type of your choice to do it at once like this:

df[df.select_dtypes(include=['object']).columns] = df[df.select_dtypes(include=['object']).columns].apply(lambda x: x.str.strip())

where you can substitute 'object' type which represents string type to 'bool', 'int64', etc.

Amirkhm
  • 948
  • 11
  • 13