【问题标题】:Pandas: How to find line breaks in a DF?Pandas:如何在 DF 中找到换行符?
【发布时间】:2021-11-09 01:49:37
【问题描述】:

我需要找到换行符的所有表示来规避 AzureML 的设计者造成的问题,如下所示:

By default (support_multi_line=False), all line breaks,
including those in quoted field values,
will be interpreted as a record break.

因此,这种设计选择通过夸大其记录并在我的管道中创建错误来破坏我的 DF。

我已经尝试过:

df.replace(to_replace=[r"\\t|\\n|\\r", "\t|\n|\r"], value=["", ""], regex=True, inplace=True)

但它不起作用——在我的 DF 中仍然可以找到换行符——我还应该寻找什么?

【问题讨论】:

  • 它出现在哪一列?

标签: python pandas azureml-python-sdk


【解决方案1】:

df.replace() 在数据框的所有行和列中搜索整个值,并将这些值替换为指定值。它不会替换部分字符串。

你正在寻找df[column].str.replace:

df[column] = df[column].str.replace('[\n|\r|\t]|\\\\[nrt]', '', regex=False)

【讨论】:

  • 抱歉耽搁了——刚开始测试。与上述[r"\\t|\\n|\\r", "\t|\n|\r"] 相比,此策略处理的换行符更少(即问题稍微严重一些)。
  • 哦,真的吗?听到这个我很惊讶。请您发送问题中的数据样本,尤其是包含错误数据的行吗?如果我能看到您实际处理的数据,我将能够提供更好的帮助
猜你喜欢
  • 2021-02-19
  • 2017-04-20
  • 2017-03-29
  • 2017-09-13
  • 2011-08-23
  • 2018-08-20
  • 2022-01-19
  • 2018-02-08
  • 2020-04-18
相关资源
最近更新 更多