简体   繁体   English

如何从 Python 中的 DataFrame 列中删除选定的特殊字符

[英]How to remove selected special characters from DataFrame column in Python

I am merging different excel files into a csv file.我正在将不同的 excel 文件合并到一个 csv 文件中。 Values in one of the columns(Length) in the source files contain single quote (eg '200, '50 etc.).源文件中列(长度)之一中的值包含单引号(例如'200、'50 等)。 Some values can also contain a period at the end(eg '200., '50., '10.3 etc.).某些值还可以在末尾包含句点(例如'200.、'50.、'10.3 等)。 I want to to remove only the single quote from the values.我只想从值中删除单引号。

Input输入

Length
=======
'2000

'100.

'10.3

Desired output期望输出

Length
=======
2000

100.

10.3

I am using the following code but somehow it also removes period(.) from the values.我正在使用以下代码,但不知何故它也从值中删除了 period(.)。 Please help.请帮忙。

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)

You can try with:您可以尝试:

df['length'].str.replace("'","")

This will remove all the single quotes in the column这将删除列中的所有单引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM