4.20. DataFrame Sort

import numpy as np
import pandas as pd

np.random.seed(0)


values = np.random.randn(6, 4)
columns = ['A', 'B', 'C', 'D']
indexes = pd.date_range('1970-01-01', periods=6)
# DatetimeIndex(['1970-01-01',
#                '1970-01-02',
#                '1970-01-03',
#                '1970-01-04',
#                '1970-01-05',
#                '1970-01-06'], dtype='datetime64[ns]', freq='D')

df = pd.DataFrame(values, index=indexes, columns=columns)

4.20.1. Sort by index

df.sort_index(ascending=False) # default axis=0
df.sort_index(ascending=False, inplace=True)

A

B

C

D

1970-01-06

1.361922

-0.827940

0.400024

0.047176

1970-01-05

0.589973

0.748417

-1.680741

0.510512

1970-01-04

-0.974425

1.327082

-0.435516

1.328745

1970-01-03

-1.308835

-0.285436

-0.757591

-0.042493

1970-01-02

0.084471

-0.932586

0.160637

-0.275183

1970-01-01

0.131926

-1.825204

-1.909562

1.274718

4.20.2. Sort by columns

df.sort_index(axis=1, ascending=False)

D

C

B

A

1970-01-01

1.274718

-1.909562

-1.825204

0.131926

1970-01-02

-0.275183

0.160637

-0.932586

0.084471

1970-01-03

-0.042493

-0.757591

-0.285436

-1.308835

1970-01-04

1.328745

-0.435516

1.327082

-0.974425

1970-01-05

0.510512

-1.680741

0.748417

0.589973

1970-01-06

0.047176

0.400024

-0.827940

1.361922

4.20.3. Sort by values

df.sort_values('B')
df.sort_values('B', inplace=True)

# można sortować po wielu kolumnach (jeżeli wartości w pierwszej będą równe)
df.sort_values(['B', 'C'])
df.sort_values(['B', 'C'])

1970-01-01

0.131926

-1.825204

-1.909562

1.274718

1970-01-02

0.084471

-0.932586

0.160637

-0.275183

1970-01-06

1.361922

-0.827940

0.400024

0.047176

1970-01-03

-1.308835

-0.285436

-0.757591

-0.042493

1970-01-05

0.589973

0.748417

-1.680741

0.510512

1970-01-04

-0.974425

1.327082

-0.435516

1.328745

4.20.3.1. Assignments

Todo

Create assignments