import numpy as np
import pandas as pd
obj=pd.Series(range(4),index=['d','a','b','c'])
obj.sort_index()
a 1
b 2
c 3
d 0
dtype: int64
frame=pd.DataFrame(np.random.randint(1,10,16).reshape((4,4)),index=['D','A','C','B'],columns=['d','a','b','c'])
frame
|
d |
a |
b |
c |
D |
7 |
6 |
9 |
2 |
A |
5 |
7 |
8 |
5 |
C |
9 |
4 |
9 |
9 |
B |
6 |
7 |
9 |
6 |
frame.sort_index()
|
d |
a |
b |
c |
A |
5 |
7 |
8 |
5 |
B |
6 |
7 |
9 |
6 |
C |
9 |
4 |
9 |
9 |
D |
7 |
6 |
9 |
2 |
frame.sort_index(axis=1)
|
a |
b |
c |
d |
D |
6 |
9 |
2 |
7 |
A |
7 |
8 |
5 |
5 |
C |
4 |
9 |
9 |
9 |
B |
7 |
9 |
6 |
6 |
frame.sort_values(by='a')
|
d |
a |
b |
c |
C |
9 |
4 |
9 |
9 |
D |
7 |
6 |
9 |
2 |
A |
5 |
7 |
8 |
5 |
B |
6 |
7 |
9 |
6 |
R语言
rownames<-c('r1','r2','r3','r4')
colnames<-c('c1','c2','c3','c4')
df<-data.frame(matrix(sample(1:10,16,replace=T),nrow=4,dimnames=list(rownames,colnames)))
df
newdf<-df[order(df$c1),]
newdf