如何从 Python 中的 DataFrame 列中删除选定的特殊字符
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
我正在使用以下代码,但不知何故它还从值中删除了句点 (.)。请帮忙。
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("'","")
这将删除列中的所有单引号
我正在将不同的 excel 文件合并到一个 csv 文件中。源文件中一列(长度)中的值包含单引号(例如 '200、'50 等)。某些值还可以在末尾包含句点(例如 '200.、'50.、'10.3 等)。我只想从值中删除单引号。
输入
Length
=======
'2000
'100.
'10.3
期望输出
Length
=======
2000
100.
10.3
我正在使用以下代码,但不知何故它还从值中删除了句点 (.)。请帮忙。
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("'","")
这将删除列中的所有单引号