对DataFrame的某一列进行操作,一般都会使用df[“xx”].str下的方法

df[ ].str.strip( )

先创建带有空格的df:

  1. df = pd.DataFrame({'Company':['company A','company B','company C','company A'],
  2. 'Sales':['Peter ',' Amy','John ','Mi ke']})
  3. df
  4. Out[38]:
  5. Company Sales
  6. 0 company A Peter
  7. 1 company B Amy
  8. 2 company C John
  9. 3 company A Mi ke

.str 先要转成字符串, lstrip()表示去除左边空格

  1. df['Sales'].str.lstrip()
  2. Out[39]:
  3. 0 Peter
  4. 1 Amy
  5. 2 John
  6. 3 Mi ke
  7. Name: Sales, dtype: object

.str 先要转成字符串, rstrip()表示去除左边空格

  1. df['Sales'].str.rstrip()
  2. Out[40]:
  3. 0 Peter
  4. 1 Amy
  5. 2 John
  6. 3 Mi ke
  7. Name: Sales, dtype: object

.str 先要转成字符串, strip()表示去除左右两边,当中的空格仍在:

  1. df['Sales'].str.strip()
  2. Out[41]:
  3. 0 Peter
  4. 1 Amy
  5. 2 John
  6. 3 Mi ke
  7. Name: Sales, dtype: object

用replace 可以替换所有的空格:

  1. df['Sales'].str.replace(' ','')
  2. Out[42]:
  3. 0 Peter
  4. 1 Amy
  5. 2 John
  6. 3 Mike
  7. Name: Sales, dtype: object

继续replace:

  1. df['Company'].str.replace('company','')
  2. Out[43]:
  3. 0 A
  4. 1 B
  5. 2 C
  6. 3 A
  7. Name: Company, dtype: object

直接用df.replace()

  1. df.replace('company A','New')
  2. Out[84]:
  3. Company Sales
  4. 0 New Peter
  5. 1 company B Amy
  6. 2 company C John
  7. 3 New Mi ke

series.str.replace() & df.replace()的区别:

  1. df['Sales'].str.replace('Amy','AAA')
  2. Out[87]:
  3. 0 Peter
  4. 1 AAA
  5. 2 John
  6. 3 Mi ke
  7. Name: Sales, dtype: object
  8. df.replace({'Amy':'AAA','company B':'BBB'})
  9. Out[90]:
  10. Company Sales
  11. 0 company A Peter
  12. 1 BBB Amy
  13. 2 company C John
  14. 3 company A Mi ke

df.replace() 当有空格的时候才会替换,并不会替换啊单元格里面的字符串后面的空格:

  1. df.replace(' ' ,'')
  2. Out[91]:
  3. Company Sales
  4. 0 company A Peter
  5. 1 company B Amy
  6. 2 company C John
  7. 3 company A Mi ke