我正在使用熊猫对视频游戏信息进行分类。我有我需要的数据,我需要的只是将结果四舍五入到最接近的整数..
# Create a data frame with the properly queried and sorted data
semi1 = (ratingSort.sort_values(by=['numVotes', 'averageRating'], ascending=False))
# Create a data frame with only the required columns
semi2 = semi1[['primaryTitle', 'startYear', 'averageRating', 'numVotes']]
# Create a data frame with a subset of records
final = (semi2.head(10))
final
(此处正确打印数据)
然后我试着四舍五入到最接近的整数:
['User_Count'].round(decimals=0)
返回错误。我也试过:
final = ['User_Count'].round(decimals=0)
列标题是“User_Count”和“Name”
错误是由于这一行引起的:
['User_Count'].round(decimals=0)
当您在“User_Count”周围使用方括号时,python 正在创建一个包含元素“User_Count”作为字符串的列表,并尝试使用它调用 round。由于列表没有名为 round 的方法,因此会引发此错误。
发生此错误是因为您正在对仅包含“User_Count”一词的列表进行调用。当您编写 ['User_Count'] 时,python 将创建一个包含一个元素的列表,该元素将是字符串 'User_Count'。你需要做的是在 pandas 数据帧上调用 round 函数。
首先,将“User_Count”列添加到您的最终数据框中:
semi2 = semi1[['primaryTitle', 'startYear', 'averageRating', 'numVotes', 'User_Count']]
final = semi2.head(10)
接下来,在结果数据帧的“User_Count”列上调用轮函数
final['User_Count'].round(decimals=0)
有关更多信息,请参阅有关 pandas.DataFrame.round 函数的文档:link