【问题标题】:Load data: TypeError: cannot convert the series to <class 'int'>加载数据:TypeError:无法将系列转换为 <class 'int'>
【发布时间】:2019-11-26 03:24:03
【问题描述】:

我目前正在从事一个情绪分析项目。当我尝试加载数据时,出现错误TypeError: '&lt;=' not supported between instances of 'str' and 'int'。所以我将str修改为int。但是,现在出现错误TypeError: cannot convert the series to &lt;class 'int'&gt;。我该如何解决这个错误? 数据类型 = dtype('O')

import pandas as pd
#read data
review_df = pd.read_csv( '/Users/mhnwong/Desktop/FYP potential/metropark_trans_demoji.csv')
#create label
review_df["is_bad_review"] = int(review_df["rate"]).apply(lambda x:1 if x <= 8 else 0)
# select only relevant columns
review_df = review_df[["review","is_bad_review"]]

review_df.head()

数据: This is the data I have got

【问题讨论】:

  • 你能粘贴metropark_trans_demoji.csv文件的内容吗?这可能会帮助我们解决您的问题。
  • 你能试试review_df["is_bad_review"] = review_df["rate"].apply(lambda x:1 if int(x) &lt;= 8 else 0)吗?

标签: python pandas sentiment-analysis


【解决方案1】:

metropark_trans_demoji.csv 文件中的 rate 列是否包含不属于数字的内容?

首先要确保rate列的内容都可以转换为int

假设rate列的数据都属于int,那么可以写

review_df["is_bad_review"] = review_df["rate"].apply(lambda x:1 if int(x) <= 8 else 0)

这会将rate 列的内容转换为int,然后将其与8 进行比较。

review_df["rate"] 的类型是&lt;class 'pandas.core.series.Series'&gt;。不能直接转成int

【讨论】:

  • 我试过但它返回 TypeError: cannot convert the series to
  • 我改变了我的答案。你能试试新的吗?
  • type(review_df["rate"])的返回值是多少?
  • 我猜type(review_df["rate"]) 的类型是&lt;class 'pandas.core.series.Series'&gt;
  • 是的,类型是 pandas.core.series.Series
猜你喜欢
  • 1970-01-01
  • 2021-09-24
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多