pandas sort_values() 得到了一个意外的关键字参数“by”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50698180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:39:10  来源:igfitidea点击:

sort_values() got an unexpected keyword argument 'by'

pythonpandasdataframe

提问by sakimarquis

for i in str_list:   #str_list is a set contain some strings 
    df.loc[i].sort_values(by = 'XXX')
**TypeError**: sort_values() got an unexpected keyword argument 'by' ".
>>> type(df.loc[i])
>>> pandas.core.frame.DataFrame

But it works outside the for loop!

但它在 for 循环之外工作!

df.loc['string'].sort_values(by = 'XXX')
>>> type(df.loc['string'])
>>> pandas.core.frame.DataFrame

I'm confused.

我糊涂了。

回答by kosnik

This is because the result of the locoperator is a pandas.Seriesobject in your case. The sort_valuesin this case doesn't have a keyword argument bybecause it can only sort the series values. Have a look at the difference in the signature when you call sort valuesin a pandas.DataFrame

这是因为在您的情况下,loc运算符的结果是一个pandas.Series对象。在sort_values这种情况下没有一个关键字参数by,因为它只能排序的序列值。有一个在签名中的差异,当你调用sort values一个pandas.DataFrame

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html

and when you call sort_valuesin a pandas.Series

当你打电话sort_valuespandas.Series

http://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.Series.sort_values.html

http://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.Series.sort_values.html

回答by Carl Kristensen

To add to the answer, why is it returning a series in one case and a data frame in another?

为了补充答案,为什么它在一种情况下返回一个系列而在另一种情况下返回一个数据框?

.loc function is returning a Series in the first case

.loc 函数在第一种情况下返回一个系列

for i in str_list: #str_list is a set contain some strings

df.loc[i].sort_values(by = 'XXX')

for i in str_list: #str_list 是一个包含一些字符串的集合

df.loc[ i].sort_values(by = 'XXX')

because the argument iappears only once in the DataFrame.

因为参数i在 DataFrame 中只出现一次。

in the second case, the 'string' is duplicated and therefore will return a DataFrame.

在第二种情况下,“字符串”被复制,因此将返回一个数据帧。

df.loc['string'].sort_values(by = 'XXX')

df.loc['string'].sort_values(by = 'XXX')

If the 'string' argument is not duplicated then note that there are also some differences if the argument in .loc is on a list. for example.

如果 'string' 参数没有重复,那么请注意,如果 .loc 中的参数在列表中,也会存在一些差异。例如。

df.loc['string'] -> returns a Series

df.loc['string'] -> 返回一个系列

df.loc[['string']] -> returns a DataFrame

df.loc[['string']] -> 返回一个 DataFrame

Maybe in the second case you are giving ['string'] as the argument instead of 'string' ?

也许在第二种情况下,您将 ['string'] 作为参数而不是 'string' ?

Hope this helps.

希望这可以帮助。