我面临TypeError:sort_values()缺少1个必需的位置参数:’by’
for i in range(0,len(data_sims.index)):
for j in range(1,len(data_sims.columns)):
user = data_sims.index[i]
serial = data_sims.columns[j]
if dataUser.ix[i][j] == 1:
data_sims.ix[i][j] = 0
else:
serial_top_names = data_neighbours.ix[serial][1:10]
serial_top_sims = dataSim.ix[serial].sort_values(ascending=False)[1:10]
user_preferences = dataUser.ix[user,serial_top_names]
data_sims.ix[i][j] = getScore(user_preferences,serial_top_sims)
详细错误:
> --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 9 else: 10 serial_top_names = data_neighbours.ix[serial][1:10] ---> 11 serial_top_sims = dataSim.ix[serial].sort_values(ascending=False)[1:10] 12 user_preferences = dataUser.ix[user,serial_top_names] 13 TypeError: sort_values() missing 1 required positional argument: 'by'
我尝试使用以下但没有工作
pd.DataFrame.sort_values(dataSim,ascending=[0])[1:10]
有什么我想念的吗?
最佳答案
documentation for Pandas提到:
DataFrame.sort_values(by,
axis=0,
ascending=True,
inplace=False,
kind='quicksort',
na_position='last')
by:str或str的列表
引用轴项目的名称或名称列表.
在我的例子中:
df.groupby('product').agg({'weight':'sum'}).sort_values(by='weight')
添加by =’weight’解决了TypeError:sort_values()缺少1个必需的位置参数:’by’