【问题标题】:Translate the newline character in a series. optimization翻译一系列中的换行符。优化
【发布时间】:2022-10-04 21:38:27
【问题描述】:

我有一系列带有转义字符的字符串,像这样

s = pd.Series(['potato\n\r','corn\n\r','pinea\n apple','\r potato\n head'])

所以我这样做是为了删除它们

s.apply(lambda s: s.translate({10:'',13 : ''}))
# Remove newline and carriage ret

问题是我不确定,如果这是最好的方法,并且由于数据很大,我想要一些输入。 要知道的是,我现实生活中的数据中的字符串不包含转义字符。过滤它,只替换有效的字符串可能比我目前这样做的方式更有效,但我不知道如何映射它。希望对这种性能改进提供一些帮助吗?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    也许使用正则表达式,替换所有出现的 或者 有一个空字符串

    s.replace(r'[
    |
    ]', '', regex=True)
    
    0          potato
    1            corn
    2     pinea apple
    3     potato head
    dtype: object
    

    【讨论】:

      猜你喜欢
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      相关资源
      最近更新 更多