为什么我的 Pandas DataFrame 不使用 `sort_values` 显示新订单? [英] Why does my Pandas DataFrame not display new order using `sort_values`?
问题描述
Pandas 的新手,所以也许我错过了一个好主意?我有一个 Pandas DataFrame 的注册事务,其形状类似于 (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屋!