【问题标题】:How can I remove emojis from a dataframe?如何从数据框中删除表情符号?
【发布时间】:2019-08-15 18:06:51
【问题描述】:

我知道

test = []
for item in my_texts:
    test.append(item.encode('ascii', 'ignore').decode('ascii'))

从列表中删除表情符号。但是如何从数据框中删除表情符号?当我尝试

a = []
for item in goldtest['Text']:
    a.append(item.encode('ascii', 'ignore').decode('ascii'))

我只得到了 goldtest 的最后一个条目。当我在整个数据框上尝试代码时,我得到 ''AttributeError: 'DataFrame' object has no attribute 'encode'''

【问题讨论】:

  • DataFrame 不是字符串。所以问问自己,你实际上调用的是什么 encode,因为你的错误表明它是一个 DataFrame
  • 这种模式不仅会删除“表情符号”,还会删除所有重音字符、非拉丁字母和标点符号以及一些更常见的符号 - 有效地破坏您拥有的任何文本数据。

标签: python dataframe emoji


【解决方案1】:

这将是 pandas 的等效代码。它逐列操作。

df.astype(str).apply(lambda x: x.str.encode('ascii', 'ignore').str.decode('ascii'))

【讨论】:

  • 使用我的数据,这给了我一个 AttributeError: ('Can only use .str accessor with string values, which use np.object_ dtype in pandas', 'occurred at index Index')。意味着 Index 中的一个值是逗号?
  • 现在试试,我编辑它以强制所有值到 str
  • 感谢您的投票。下次如果您可以添加一个简单的可重现示例,它将更加有用:D
  • 效果很好,但不幸的是,它还会延迟所有波兰语特殊字符,如 ą、ę、ń、ó、ż... 你知道如何克服吗?
猜你喜欢
  • 1970-01-01
  • 2021-09-10
  • 2015-03-17
  • 1970-01-01
  • 2012-10-20
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多