对DataFrame的某一列进行操作,一般都会使用df[“xx”].str下的方法
df[ ].str.strip( )
先创建带有空格的df:
df = pd.DataFrame({'Company':['company A','company B','company C','company A'],
'Sales':['Peter ',' Amy','John ','Mi ke']})
df
Out[38]:
Company Sales
0 company A Peter
1 company B Amy
2 company C John
3 company A Mi ke
.str 先要转成字符串, lstrip()表示去除左边空格
df['Sales'].str.lstrip()
Out[39]:
0 Peter
1 Amy
2 John
3 Mi ke
Name: Sales, dtype: object
.str 先要转成字符串, rstrip()表示去除左边空格
df['Sales'].str.rstrip()
Out[40]:
0 Peter
1 Amy
2 John
3 Mi ke
Name: Sales, dtype: object
.str 先要转成字符串, strip()表示去除左右两边,当中的空格仍在:
df['Sales'].str.strip()
Out[41]:
0 Peter
1 Amy
2 John
3 Mi ke
Name: Sales, dtype: object
用replace 可以替换所有的空格:
df['Sales'].str.replace(' ','')
Out[42]:
0 Peter
1 Amy
2 John
3 Mike
Name: Sales, dtype: object
继续replace:
df['Company'].str.replace('company','')
Out[43]:
0 A
1 B
2 C
3 A
Name: Company, dtype: object
直接用df.replace()
df.replace('company A','New')
Out[84]:
Company Sales
0 New Peter
1 company B Amy
2 company C John
3 New Mi ke
series.str.replace() & df.replace()的区别:
df['Sales'].str.replace('Amy','AAA')
Out[87]:
0 Peter
1 AAA
2 John
3 Mi ke
Name: Sales, dtype: object
df.replace({'Amy':'AAA','company B':'BBB'})
Out[90]:
Company Sales
0 company A Peter
1 BBB Amy
2 company C John
3 company A Mi ke
df.replace() 当有空格的时候才会替换,并不会替换啊单元格里面的字符串后面的空格:
df.replace(' ' ,'')
Out[91]:
Company Sales
0 company A Peter
1 company B Amy
2 company C John
3 company A Mi ke