【问题标题】:TypeError: sort_index() got an unexpected keyword argument 'by' error in pandas PythonTypeError:sort_index() 在 pandas Python 中得到了一个意外的关键字参数“by”错误
【发布时间】:2021-04-10 11:15:03
【问题描述】:

不浪费时间,直奔问题。 我正在尝试对我的数据集进行排序并从该 mean_ratings DataFrame 中获取“top_female_ratings”。 我运行了这段代码:

active_titles = ratings_by_title.index[ratings_by_title >= 250]
active_titles[:10]

mean_ratings = mean_ratings.loc[active_titles]
mean_ratings.info()

这段代码给了我这个输出:

<class 'pandas.core.frame.DataFrame'>
Index: 1216 entries, 'burbs, The (1989) to eXistenZ (1999)
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   F       1216 non-null   float64
 1   M       1216 non-null   float64
dtypes: float64(2)
memory usage: 28.5+ KB

所以,我想过滤并从上方获取顶部的“F”列: 我写了这段代码:

top_female_ratings = mean_ratings.sort_index(ascending=False, by='F')
top_female_ratings[:10]

我得到了这个错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-8-0f1195012e3e> in <module>
----> 1 top_female_ratings = mean_ratings.sort_index(ascending=False, by='F')
      2 top_female_ratings[:10]

TypeError: sort_index() got an unexpected keyword argument 'by'

我没听懂。

【问题讨论】:

  • 您在寻找sort_values 吗?

标签: python pandas dataframe machine-learning scikit-learn


【解决方案1】:

根据文档 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_index.html

DataFrame.sort_index(axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True, ignore_index=False, key=None)

"by" 不是 sort_index 函数的参数。 一种可能的方法是

top_female_ratings = mean_ratings['F'].sort_index(ascending=False) top_female_ratings[:10]

我是初学者,如果我错了请纠正我

【讨论】:

  • 是的,兄弟,你是对的,但以另一种方式。 'by' 是几年前 sort_index() 的参数,但知道它已被删除。我只是从代码中删除它并在没有它的情况下运行上面的代码,我得到了我的结果。我也运行了您提供的代码,但您的代码给出了不同的结果。
【解决方案2】:

如果有人在寻找答案:

by 现已从 sort_index 中删除,如果您关注 Python 数据科学书籍,您可以使用 sort_values 代替 sort_index 用于获得所需的输出。

top_female_ratings = mean_ratings.sort_values(by='F',ascending=False)
top_female_ratings[:10]

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    相关资源
    最近更新 更多