首页 > 解决方案 > 处理 pandas 的“尝试使用 .iloc”错误

问题描述

我正在计算我的书评数据框中每个用户的评分数,方法是:

df_new['ratings_per_user']=df_new.groupby('userID')['userID'].transform('count')

这似乎可行,但会引发错误(或者更确切地说,是警告):

/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  """Entry point for launching an IPython kernel.

我不明白如何使用 重写上述命令.iloc,更一般地说,实际问题是什么。

标签: pandasdataframepandas-groupby

解决方案


推荐阅读