python sort、sort_index方法(排序)
2018-01-10 17:46
411 查看
对Series进行排序
#生成序列obj obj=pd.Series([4,9,6,20,4],index=['d','a','e','b','c']) d 4 a 9 e 6 b 20 c 4 dtype: int64 #按obj的索引排序,默认升序,降序可在括号加ascending=False obj.sort_index() a 9 b 20 c 4 d 4 e 6 dtype: int64 #按obj的值排序,默认升序 obj.order() d 4 c 4 e 6 a 9 b 20 dtype: int641
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[/code]
对DataFrame进行排序
#生成frame frame=pd.DataFrame(pd.Series([3,5,2,6,9,23,12,34,12,15,11,0]).reshape(3,4),columns=['c','f','d','a'],index=['C','A','B']) c f d a C 3 5 2 6 A 9 23 12 34 B 12 15 11 0 #按frame的行索引进行排序 frame.sort_index() c f d a A 9 23 12 34 B 12 15 11 0 C 3 5 2 6 #按frame的列索引进行排序 frame.sort_index(axis=1) a c d f C 6 3 2 5 A 34 9 12 23 B 0 12 11 15 #按frame的一个列或多个列的值进行排序 frame.sort_index(by='a') c f d a B 12 15 11 0 C 3 5 2 6 A 9 23 12 34 frame.sort_index(by=['a','c']) c f d a B 12 15 11 0 C 3 5 2 6 A 9 23 12 34
相关文章推荐
- python sort、sort_index方法(排序)
- python sort、sort_index方法(排序)
- python sort、sort_index方法(排序)
- python 列表排序方法reverse、sort、sorted详解
- python 列表排序方法sort、sorted技巧篇
- Python 列表排序方法reverse、sort、sorted详解
- 基于python list对象中嵌套元组使用sort时的排序方法
- Python 列表排序方法reverse、sort、sorted详解
- Python 列表排序方法reverse、sort、sorted详解
- Python排序(sort,sorted)保存原来index
- Python实现二维数组按照某行或列排序的方法【numpy lexsort】
- python 列表排序方法sort、sorted技巧篇
- python 列表排序方法sort、sorted技巧篇(转自: aben)
- Python中的排序方法(Bubble Sort,Insert Sort,Select Sort,Merge Sort,Quick Sort)
- Python 列表排序方法reverse、sort、sorted详解
- python 列表排序方法reverse、sort、sorted基础篇
- python中列表排序sort()方法或者内建函数sorted()
- 在Python中使用sort()方法进行排序的简单教程
- Python 列表的sort()方法之高级排序
- python中Series 和 DataFrame 中的sort_index 方法