当前位置:   article > 正文

pandas排序_pandas chunksize 排序

pandas chunksize 排序

pandas排序


pandas支持三种排序方式:

  • sorting by index labels
  • sorting by column values
  • sorting by a combination of both

By index

series.sort_indexDataFrame.sort_index()方法被用来根据index排序pandas对象。

import numpy as np
import pandas as pd
  • 1
  • 2
df = pd.DataFrame({
    'one':pd.Series(np.random.randn(3),index=['a','b','c']),
     'two': pd.Series(np.random.randn(4), index=['a', 'b', 'c', 'd']),
     'three': pd.Series(np.random.randn(3), index=['b', 'c', 'd'])                                       
})
  • 1
  • 2
  • 3
  • 4
  • 5
unsorted_df = df.reindex(index=['a','d','c','b'],
                                columns=['three','teo','one'])
  • 1
  • 2
unsorted_df
  • 1
threeteoone
aNaNNaN1.474084
d2.021092NaNNaN
c0.057446NaN-0.085553
b0.705659NaN-0.101295
unsorted_df.sort_index()
  • 1
threeteoone
aNaNNaN1.474084
b0.705659NaN-0.101295
c0.057446NaN-0.085553
d2.021092NaNNaN
unsorted_df.sort_index(ascending=False)
  • 1
threeteoone
d2.021092NaNNaN
c0.057446NaN-0.085553
b0.705659NaN-0.101295
aNaNNaN1.474084
unsorted_df.sort_index(axis=1) # 设置排序的轴
  • 1
oneteothree
a1.474084NaNNaN
dNaNNaN2.021092
c-0.085553NaN0.057446
b-0.101295NaN0.705659

By values

Series.sort_values()方法按照Series的值排序。

DataFrame.sort_values()方法按照其columns或rows的值排序。

df1 = pd.DataFrame({
    'one':[2,1,1,1],
    'two':[1,2,3,4],
    'three':[5,4,3,2]
})
  • 1
  • 2
  • 3
  • 4
  • 5
df1.sort_values(by='two')
  • 1
onetwothree
0215
1124
2133
3142
df1.sort_values(by=['one','two'])
  • 1
onetwothree
1124
2133
3142
0215

这些方法通过na_position参数对NA值进行特殊处理:

 s = pd.Series(['a', 'a', 'b', 'b', 'a', 'a', np.nan, 'c', 'd', 'a'])
  • 1
s[2] = np.nan
  • 1
s.sort_values()
  • 1
0      a
1      a
4      a
5      a
9      a
3      b
7      c
8      d
2    NaN
6    NaN
dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
s.sort_values(na_position='first')
  • 1
2    NaN
6    NaN
0      a
1      a
4      a
5      a
9      a
3      b
7      c
8      d
dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

By indexes and values

作为by参数传递给DataFrame.sort_values()的字符串可以引用列名或索引级名。

 idx = pd.MultiIndex.from_tuples([('a', 1), ('a', 2), ('a', 2),('b', 2), ('b', 1), ('b', 1)])
  • 1
idx.names = ['first', 'second']
  • 1
idx
  • 1
MultiIndex([('a', 1),
            ('a', 2),
            ('a', 2),
            ('b', 2),
            ('b', 1),
            ('b', 1)],
           names=['first', 'second'])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
df_multi = pd.DataFrame({'A': np.arange(6, 0, -1)},index=idx)
  • 1
df_multi
  • 1
A
firstsecond
a16
25
24
b23
12
11
df_multi.sort_values(by=['second', 'A'])
  • 1
A
firstsecond
b11
12
a16
b23
a24
25

如果字符串与列名和索引级名称都匹配,则会发出警告,并且列优先。

搜索排序

Series的serachsorted()方法工作方式与numpy.ndarray.serachsorted()方法类似。

该方式表示将传入的数组的每一个元素插入到Series对象中,返回插入的位置。(实际上并没有插入Serives,只是求,如果插入,那么插入的位置是什么。)

ser = pd.Series([1, 2, 3])
  • 1
ser.searchsorted([0,3])
  • 1
array([0, 2], dtype=int64)
  • 1
ser.searchsorted([0, 4])
  • 1
array([0, 3], dtype=int64)
  • 1
ser.searchsorted([1, 3], side='right')
  • 1
array([1, 3], dtype=int64)
  • 1
ser.searchsorted([1, 3], side='left')
  • 1
array([0, 2], dtype=int64)
  • 1
ser = pd.Series([3, 1, 2])
ser.searchsorted([0, 3], sorter=np.argsort(ser))
  • 1
  • 2
array([0, 2], dtype=int64)
  • 1

最小/最大值

Series有nsmallest()nlargest()方法,它们返回最大或者最小的前n个值。对于一个大的Series,大的速度要比排序整个Series要快的多。

s = pd.Series(np.random.permutation(10))

s
  • 1
  • 2
  • 3
0    9
1    2
2    8
3    6
4    0
5    7
6    3
7    4
8    1
9    5
dtype: int32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
s.sort_values()
  • 1
4    0
8    1
1    2
6    3
7    4
9    5
3    6
5    7
2    8
0    9
dtype: int32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
s.nsmallest(4)
  • 1
4    0
8    1
1    2
6    3
dtype: int32
  • 1
  • 2
  • 3
  • 4
  • 5
s.nlargest(3)
  • 1
0    9
2    8
5    7
dtype: int32
  • 1
  • 2
  • 3
  • 4

DataFrame同样也有nlargest()nsmallist()方法

df = pd.DataFrame({
    'a':[-2,-1,1,10,8,11,-1],
    'b':list('abdceff'),
    'c':[1.,2.,4.,3.2,np.nan,3.,4.0]
})
  • 1
  • 2
  • 3
  • 4
  • 5
df.nlargest(3,'a')
  • 1
abc
511f3.0
310c3.2
48eNaN
df.nlargest(5,['a','c'])
  • 1
abc
511f3.0
310c3.2
48eNaN
21d4.0
6-1f4.0

根据多重索引排序

当column是多重索引时,必须明确指明排序依据的levels。

df1
  • 1
onetwothree
0215
1124
2133
3142
df1.columns = pd.MultiIndex.from_tuples([
    ('a','one'),
    ('a','two'),
    ('b','three')
])
  • 1
  • 2
  • 3
  • 4
  • 5
df1
  • 1
ab
onetwothree
0215
1124
2133
3142
df1.sort_values(by=('a','two'))
  • 1
ab
onetwothree
0215
1124
2133
3142

  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/162034
推荐阅读
相关标签
  

闽ICP备14008679号