어떤 기준에 근거해서 데이터를 정렬하는 것 역시 중요한 명령이다. 로우나 컬럼의 색인을 알
파벳순으로 정렬하려면 정렬된 새로운 객체를 반환하는
sort
_
index
메서드를 사용하면 된다.
In
[
201
]:
obj
=
pd
.
Series
(
range
(
4
),
index
=
['
d
',
'
a
',
'
b
',
'
c
'])
In
[
202
]:
obj
.
sort
_
index
()
Out
[
202
]:
a 1
b 2
c 3
d 0
dtype
:
int64
DataFrame
은 로우나 컬럼 중 하나의 축을 기준으로 정렬할 수 있다.
In
[
203
]:
frame
=
pd
.
DataFrame
(
np
.
arange
(
8
).
reshape
((
2
,
4
)),
.....:
index
=
['
three
',
'
one
'],
.....:
columns
=
['
d
',
'
a
',
'
b
',
'
c
'])
In
[
204
]:
frame
.
sort
_
index
()
Out
[
204
]:
d a b c
one 4 5 6 7
three 0 1 2 3
In
[
205
]:
frame
.
sort
_
index
(
axis
=
1
)
Out
[
205
]:
a b c d
three 1 2 3 0
one 5 6 7 4
데이터는 기본적으로 오름차순으로 정렬되고 내림차순으로 정렬할 수도 있다.
In
[
206
]:
frame
.
sort
_
index
(
axis ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.