data1 = pd.DataFrame(np.random.randint(0,9,[5,5]),
index=list('abcde'),
columns=list('ABCDE'))
ascending默认True 升序
axis默认0 行索引
data1.sort_index(ascending=False,axis=1)
data1.sort_values('E')
data1.T.sort_values(by='a').T
data1.sort_values(by='a',axis=1,ascending=False)
ValueError: When sorting by column, axis must be 0 (rows)
data1.apply(np.sort,axis = 1)
ser1 = pd.Series([3,1,5,2,5],index=list('abcde'))
ser1
a 3
b 1
c 5
d 2
e 5
dtype: int64
参数 method 默认='average' max、min 共用最大最小;first 第一次出现的排名靠前
ser1.rank(method='first')
a 3.0
b 1.0
c 4.0
d 2.0
e 5.0
dtype: float64
apple_share = pd.Series(np.random.randint(1000,3400,12))
apple_share
0 1509
1 2806
2 1030
3 1084
4 3023
5 3098
6 3289
7 3003
8 1544
9 2442
10 1226
11 1767
dtype: int32
默认以天生成
以月份来生成 freq='M' 天=D
如果不确定结束时间,可以制定生成多少个 periods
apple_index = pd.date_range(start='20170101',periods=12,freq="M")
apple_index = pd.date_range(start='20170101',end='20171231',freq="M")
apple_index
DatetimeIndex(['2017-01-31', '2017-02-28', '2017-03-31', '2017-04-30',
'2017-05-31', '2017-06-30', '2017-07-31', '2017-08-31',
'2017-09-30', '2017-10-31', '2017-11-30', '2017-12-31'],
dtype='datetime64[ns]', freq='M')
apple_share.index = apple_index
apple_share
2017-01-31 1509
2017-02-28 2806
2017-03-31 1030
2017-04-30 1084
2017-05-31 3023
2017-06-30 3098
2017-07-31 3289
2017-08-31 3003
2017-09-30 1544
2017-10-31 2442
2017-11-30 1226
2017-12-31 1767
Freq: M, dtype: int32