0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DataFrameの中身を等間隔で取り出す

Last updated at Posted at 2021-10-09

試験データ、例えば引張試験時の変位と荷重をとても細かいサンプリング間隔で取得してしまうと、その後のデータ処理やグラフ化する際にファイルサイズが大きすぎて扱いにくくなる場合があります。そこで、機械的に等間隔でデータを間引く方法をメモしておきます。

仮に、100行のデータを10分の1に間引きたいとします。まずは100行分のダミーデータをDataFrame型で作成します。

import numpy as np
import pandas as pd

# データ作成
np.random.seed(seed=1)
df = pd.DataFrame(np.random.rand(100, 2),
                  columns=['displacement', 'load'])

つぎのようなデータができます。

    elongation      load
0     0.417022  0.720324
1     0.000114  0.302333
2     0.146756  0.092339
3     0.186260  0.345561
4     0.396767  0.538817
..         ...       ...
95    0.263297  0.065961
96    0.735066  0.772178
97    0.907816  0.931972
98    0.013952  0.234362
99    0.616778  0.949016

[100 rows x 2 columns]

DataFrameのインデックスラベルをスライス構文で指定することで、等間隔で行を取り出すことができます。

# 一定間隔で取り出し
step = 10
df_selected = df.loc[::step]

ここで、「::」に続く数字が、取り出しのステップとなります。取り出し後のDataFrame(df_selected)の中身は以下のようになります。

    elongation      load
0     0.417022  0.720324
10    0.800745  0.968262
20    0.988861  0.748166
30    0.102334  0.414056
40    0.883306  0.623672
50    0.326645  0.527058
60    0.019880  0.026211
70    0.556240  0.136455
80    0.239848  0.493770
90    0.629718  0.210174

もともと100行あったものが、10行になりました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?