【问题标题】:Python & Pandas: 'Series' objects are mutable, thus they cannot be hashedPython & Pandas:“系列”对象是可变的,因此它们不能被散列
【发布时间】:2021-07-26 21:51:58
【问题描述】:

使用 Python 和 Pandas,我正在寻求编写一个脚本,该脚本从 text 列中获取数据,使用 textstat 模块评估该文本,然后将结果写回 word_countcolumn 下的 csv .

这是csv的结构:

 user_id         text text_number  word_count
0       10  test text A      text_0         NaN
1       11          NaN         NaN         NaN
2       12          NaN         NaN         NaN
3       13          NaN         NaN         NaN
4       14          NaN         NaN         NaN
5       15  test text B      text_1         NaN

这是我尝试将 text 列循环到 textstat 的代码:

df = pd.read_csv("texts.csv").fillna('')
text_data = df["text"]
length1 = len(text_data)

for x in range(length1):
    (text_data[x])

    #this is the textstat word count operation
    word_count = textstat.lexicon_count(text_data, removepunct=True)
    output_df = pd.DataFrame({"word_count":[word_count]})
    output_df.to_csv('texts.csv', mode="a", header=False, index=False)

但是,我收到此错误:

TypeError: 'Series' objects are mutable, thus they cannot be hashed

关于如何进行的任何建议?感谢所有帮助。

【问题讨论】:

  • 您是否可能打算传递字符串 text_data[x] 而不是系列? word_count = textstat.lexicon_count(text_data[x], removepunct=True)
  • 您希望如何处理 NaN 值?
  • 非常感谢 - 它看起来不像。运行该代码产生了这个错误:~TypeError: expected string or bytes-like object~
  • 对。因为您在 float64 的列中有 NaN。那么您希望如何处理这些问题?
  • 很好 - 我编辑了上面的代码以包含 fillna('')。使用test_data[x] 再次运行代码是成功的,尽管它把它放在user_id 列而不是word_count 中。我应该将 header 值更改为 True?

标签: python pandas csv


【解决方案1】:

更多的pandas 方法是使用fillna + apply。然后把Series直接写出to_csv

(
    df["text"].fillna('')  # Replace NaN with empty String
        .apply(textstat.lexicon_count,
               removepunct=True)  # Call lexicon_count on each value
        .rename('word_count')  # Rename Series
        .to_csv('texts.csv', mode="a", index=False)  # Write to csv
)

文本.csv:

word_count
1
0
0
0
0
1

将一列添加到现有的 DataFrame/csv 而不是附加到它的末尾也可以这样做:

df['word_count'] = (
    df["text"].fillna('')  # Replace NaN with empty String
        .apply(textstat.lexicon_count,
               removepunct=True)  # Call lexicon_count on each value
)

df.to_csv('texts.csv', index=False)  # Write to csv

文本.csv:

user_id,text,text_number,word_count
text,A,text_0,1
,,,0
,,,0
,,,0
,,,0
text,B,text_1,1

要修复当前的实现,也可以使用fillna 并有条件地仅在第一次迭代时写入标头:

text_data = df["text"].fillna('')

for i, x in enumerate(text_data):
    # this is the textstat word count operation
    word_count = textstat.lexicon_count(x, removepunct=True)
    output_df = pd.DataFrame({"word_count": [word_count]})
    output_df.to_csv('texts.csv', mode="a", header=(i == 0), index=False)

文本.csv:

word_count
1
0
0
0
0
1

DataFrame 和导入:

import pandas as pd
import textstat
from numpy import nan

df = pd.DataFrame({
    'user_id': ['text', nan, nan, nan, nan, 'text'],
    'text': ['A', nan, nan, nan, nan, 'B'],
    'text_number': ['text_0', nan, nan, nan, nan, 'text_1'],
    'word_count': [nan, nan, nan, nan, nan, nan]
})

【讨论】:

  • 非常感谢您提供的帮助。如何将数据附加到word_count 列?
  • 查看“将列添加到现有 DataFrame/csv 而不是附加到其末尾”下的第二个代码块
  • 成功!非常感谢耐心的讲解,很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多