LoginSignup
5
5

More than 3 years have passed since last update.

pandas.DataFrameの行を間引く(ダウンサンプリング)

Posted at

方法

リストと同様にスライスが使えます。

# 5行間隔で間引く
df[::5]
# 10行間隔で間引く
df[::10]

In [1]: import pandas as pd

In [2]: df = pd.DataFrame()

In [3]: df['col'] = range(100)

In [4]: df
Out[4]:
    col
0     0
1     1
2     2
3     3
4     4
..  ...
95   95
96   96
97   97
98   98
99   99

[100 rows x 1 columns]

In [5]: df[::5]
Out[5]:
    col
0     0
5     5
10   10
15   15
20   20
25   25
30   30
35   35
40   40
45   45
50   50
55   55
60   60
65   65
70   70
75   75
80   80
85   85
90   90
95   95

In [6]: df[::10]
Out[6]:
    col
0     0
10   10
20   20
30   30
40   40
50   50
60   60
70   70
80   80
90   90
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5