为什么我的Pandas DataFrame不使用`sort_values`显示新订单? [英] Why does my Pandas DataFrame not display new order using `sort_values`?
问题描述
Pandas的新手,所以也许我错过了一个大主意?
我有一个形状像(500,4)
:
New to Pandas, so maybe I'm missing a big idea?
I have a Pandas DataFrame of register transactions with shape like (500,4)
:
Time datetime64[ns]
Net Total float64
Tax float64
Total Due float64
我正在使用 Python3 Jupyter笔记本处理代码.我无法对任何列进行排序.通过不同的代码示例进行排序,检查df时看不到输出重新排序.因此,我已将问题减少为尝试仅订购一列:
I'm working through my code in a Python3 Jupyter notebook. I can't get past sorting any column. 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:
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.
考虑到这可能是Jupyter的事情,我已经使用print(df)
,df.head()
和HTML(df.to_html())
预览了结果(最后一个示例是针对Jupyter笔记本的).我还将整个笔记本从导入CSV重新运行到此代码.而且,我也是Python3的新手(从2.7开始),因此有时会遇到这种情况,但是在这种情况下我看不出这有什么用.
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.
另一篇帖子也有类似的问题, Python pandas数据框sort_values不起作用.在这种情况下,排序是在列类型string
上进行的.但是正如您所看到的,这里的所有列都是明确可排序的.
Another post has 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.
为什么我的Pandas DataFrame不使用sort_values
显示新订单?
Why does my Pandas DataFrame not display new order using sort_values
?
推荐答案
df.sort_values(['Total Due'])
返回已排序的DF,但不会就地更新DF.
df.sort_values(['Total Due'])
returns a sorted DF, but it doesn't update DF in place.
因此要明确地这样做:
df = df.sort_values(['Total Due'])
或
df.sort_values(['Total Due'], inplace=True)
这篇关于为什么我的Pandas DataFrame不使用`sort_values`显示新订单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!