![](/img/trans.png)
[英]How to remove all special characters and letters from column in DataFrame in Python Pandas?
[英]How to remove selected special characters from DataFrame column in Python
我正在將不同的 excel 文件合並到一個 csv 文件中。 源文件中列(長度)之一中的值包含單引號(例如'200、'50 等)。 某些值還可以在末尾包含句點(例如'200.、'50.、'10.3 等)。 我只想從值中刪除單引號。
輸入
Length
=======
'2000
'100.
'10.3
期望輸出
Length
=======
2000
100.
10.3
我正在使用以下代碼,但不知何故它也從值中刪除了 period(.)。 請幫忙。
import pandas as pd
import glob
path= input("Enter the location of files ")
GLB_DM_VER = input("Enter global DM version")
GLB_DM_ENV = input("Enter the global DM version environment")
file_list = glob.glob(path+"\*.xls")
excels = [pd.ExcelFile(name) for name in file_list]
frames = [x.parse(x.sheet_names[2], header=0,index_col=None) for x in excels]
combined = pd.concat(frames)
**combined['LENGTH'].replace(regex=True,inplace=True,to_replace=r'\'',value=r'')**
combined.to_csv("STAND_2.csv", header=['Global_DM_VERSION_ID','Global_DM_VERSION_ENV','TARGET_DOMAIN','SOURCE_DOMAIN','DOMAIN_LABEL','SOURCE_VARIABLE','RAVE_LABEL','TYPE','VARIABLE_LENGTH','CONTROL_TYPE','CODELIST_OID','TARGET_VARIABLE','MANDATORY','RAVE_ORIGIN'], index=False)
您可以嘗試:
df['length'].str.replace("'","")
這將刪除列中的所有單引號
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.