使用pandas从数据框中删除空间

时间:2017-05-14 19:14:01

标签: python pandas

我在以下框架中有以下数据:         UMLS

134 / head pain   /mental
135/ dizzy   /finding 
136  /dizzy a bit  / symptom
138 / severe nausea / sign

我打算删除“/”之前和之后的空格。输出应该是这样的:

     umls
134/head pain/mental
135/dizzy/finding 
136/dizzy a bit /symptom
138/severe nausea/sign

我使用了以下代码:

df ['umls'] = df_['umls'].replace (' /', '/').replace('/ ', '/')

但它不起作用。有帮助吗?

1 个答案:

答案 0 :(得分:1)

如果/需要replace个空格,请使用正则表达式\s*/\s*'\s*表示零个或多个空格):

df_['umls'] = df_['umls'].replace('\s*/\s*', '/', regex=True)
print (df_)
                      umls
0     134/head pain/mental
1        135/dizzy/finding
2  136/dizzy a bit/symptom
3   138/severe nausea/sign

也有效str.replace

df_['umls'] = df_['umls'].str.replace('\s*/\s*', '/')
print (df_)
                      umls
0     134/head pain/mental
1        135/dizzy/finding
2  136/dizzy a bit/symptom
3   138/severe nausea/sign

如有必要,请删除开头或结尾字符串中的空格,然后添加str.strip

df_['umls'] = df_['umls'].str.replace('\s*/\s*', '/')
print (df_)
                      umls
0     134/head pain/mental
1       135/dizzy/finding 
2  136/dizzy a bit/symptom
3   138/severe nausea/sign

df_['umls'] = df_['umls'].str.strip()
print (df_)
                      umls
0     134/head pain/mental
1        135/dizzy/finding
2  136/dizzy a bit/symptom
3   138/severe nausea/sign

示例数据:

df_ = pd.DataFrame({'umls': ['134 / head pain   /mental', '135/ dizzy   /finding', '136  /dizzy a bit  / symptom', '138 / severe nausea / sign']})