pandas - pd.series 会对字典键进行排序吗?
问题描述
我正在使用 Jake Vaderplas 提供的 Jupyter notebook,“Python 数据科学手册”,它说:数据可以是一个字典,其中索引默认为排序的字典键
但是当我运行代码时,输出没有按字典键排序。我在这里想念什么?
在 :
pd.Series({2:'a', 1:'b', 3:'c'})
出去 :
2 a
1 b
3 c
dtype :object
虽然预期的输出应该是
在 :
pd.Series({2:'a', 1:'b', 3:'c'})
出去 :
1 b
2 a
3 c
dtype :object
解决方案
首先要说明一下,pandas 系列sorted
默认是没有的。但是如果你想制作一个应该sorted
按索引的系列,
sr = pd.Series({2:'a', 1:'b', 3:'c'}).sort_index()
sr
输出:
1 b
2 a
3 c
这将为您提供所需的结果。
推荐阅读
- python - dataFrame 列中的空 Na
- javascript - 如何动态更改网页内容?
- angular - Heroku Node.js Angular 构建失败
- android - Android Room Select @Relation Annotation with conditions one-to-many issue
- java - JavaFX 多任务
- ios - 您如何创建一个必须对 firebase 数据库进行查询并将查询值作为 int 返回的函数?
- git - 使用 'git commit' 命令打开 Vim 而不是使用 'vim' 命令时出现未知函数错误
- mysql - 查询多对多给我零结果
- json - 如何将 ajax json 数据绑定到自定义结构类型?
- java - Java - 如何比较对象列表中的元素