Python 为什么我的 Pandas DataFrame 不使用 `sort_values` 显示新订单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42613581/
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
Why does my Pandas DataFrame not display new order using `sort_values`?
提问by xtian
New to Pandas, so maybe I'm missing a big idea?
I have a Pandas DataFrame of register transactions with shape like (500,4)
:
Pandas 的新手,所以也许我错过了一个好主意?我有一个 Pandas DataFrame 的注册事务,其形状如下(500,4)
:
Time datetime64[ns]
Net Total float64
Tax float64
Total Due float64
I'm working through my code in a Python3 Jupyter notebook. I can't get past sorting anycolumn. Working through the different code examples for sort, I'm not seeing the output reorder when I inspect the df. So, I've reduced the problem to trying to order just one column:
我正在Python3 Jupyter notebook 中处理我的代码。我无法通过对任何列进行排序。通过不同的代码示例进行排序,当我检查 df 时,我没有看到输出重新排序。因此,我已将问题简化为仅订购一列:
df.sort_values(by='Time')
# OR
df.sort_values(['Total Due'])
# OR
df.sort_values(['Time'], ascending=True)
No matter which column title, or which boolean argument I use, the displayed results never change order.
无论我使用哪个列标题或哪个布尔参数,显示的结果都不会改变顺序。
Thinking it could be a Jupyter thing, I've previewed the results using print(df)
, df.head()
, and HTML(df.to_html())
(the last example is for Jupyter notebooks). I've also rerun the whole notebook from import CSV to this code. And, I'm also new to Python3 (from 2.7), so I get stuck with that sometimes, but I don't see how that's relevant in this case.
我以为这可能是一个Jupyter的事情,我已经使用预览的结果print(df)
,df.head()
和HTML(df.to_html())
(最后一个例子是Jupyter笔记本电脑)。我还将整个笔记本从导入 CSV 重新运行到此代码。而且,我也是 Python3 的新手(从 2.7 开始),所以有时我会陷入困境,但我不明白在这种情况下这有什么关系。
Another posthas a similar problem, Python pandas dataframe sort_values does not work. In that instance, the ordering was on a column type string
. But as you can see all of the columns here are unambiguously sortable.
另一个帖子也有类似的问题,Python pandas dataframe sort_values does not work。在那种情况下,排序是在列类型上string
。但是正如您所看到的,这里的所有列都是明确可排序的。
Why does my Pandas DataFrame not display new order using sort_values
?
为什么我的 Pandas DataFrame 不使用 显示新订单sort_values
?
回答by MaxU
df.sort_values(['Total Due'])
returns a sorted DF, but it doesn't update DF in place.
df.sort_values(['Total Due'])
返回一个排序的 DF,但它不会就地更新 DF。
So do it explicitly:
所以明确地做:
df = df.sort_values(['Total Due'])
or
或者
df.sort_values(['Total Due'], inplace=True)
回答by gherson
My problem, fyi, was that I wasn't returning the resulting dataframe, so PyCharm wasn't bothering to update said dataframe. Naming the dataframe after the return keyword fixed the issue.
我的问题,仅供参考,是我没有返回结果数据帧,所以 PyCharm 没有费心更新所述数据帧。在 return 关键字之后命名数据帧解决了这个问题。
Edit:
I had return
at the end of my method instead of
return df
,
which the debugger must of noticed, because df
wasn't being updated in spite of my explicit, in-place sort.
编辑:我return
在我的方法的末尾而不是
return df
,调试器必须注意到这df
一点,因为尽管我进行了明确的就地排序,但并没有被更新。