【问题标题】:deleting all specifics characters from xlsx file with python使用python从xlsx文件中删除所有特定字符
【发布时间】:2021-03-14 16:05:23
【问题描述】:

我需要删除所有 ;和 ' 来自我正在使用的 xlsx 文件,我一直在尝试使用 pandas 但它不太有效,我的代码看起来像这样

import pandas

path = r"C:\Users\gb2gaet\Nova pasta\data.xlsx"

df= pandas.read_excel(path)

for column in df.columns:
    df[column] = df[column].replace(';', "")


df.to_excel("new.xlsx")

我以前做过这个,但是用数值,它工作得很好,但是当我运行这段代码然后我去检查文件时,它仍然有不需要的 ; 我错过了什么?

【问题讨论】:

    标签: python excel pandas


    【解决方案1】:

    pandas.Series.str.replace

    df[column] = df[column].str.replace(';', "")
    

    【讨论】:

    • 我收到此错误消息:Can only use .str accessor with string values, which use np.object_ dtype in pandas
    • 或者将替换限制为字符串列而不是所有列。
    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 2020-08-20
    • 2011-09-05
    • 2016-01-02
    • 1970-01-01
    • 2013-08-08
    • 2010-10-28
    • 2015-09-05
    相关资源
    最近更新 更多