3
3

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 5 years have passed since last update.

【Python】DataFrameでシリアル番号ごとに処理したい

Last updated at Posted at 2016-07-19

次のようにシリアル番号ごとに時系列データを持つような場合に,シリアル番号ごとに処理をしたいときのループの書き方。

import pandas as pd

df = pd.read_csv('sample.csv')
df
          Date  Serial_No      Var1      Var2      Var3
0   2016-04-01          1  0.708636  0.014882  0.765210
1   2016-04-02          1  0.211528  0.012070  0.071375
2   2016-04-03          1  0.745620  0.211446  0.593242
3   2016-04-04          1  0.378551  0.249341  0.899383
4   2016-04-05          1  0.692113  0.800209  0.573121
5   2016-04-01          2  0.942247  0.047866  0.476508
6   2016-04-02          2  0.440591  0.098763  0.998441
7   2016-04-03          2  0.339409  0.784255  0.918570
8   2016-04-04          2  0.867678  0.691103  0.820591
9   2016-04-05          2  0.281377  0.627040  0.328517
10  2016-04-01          3  0.493448  0.628469  0.728271
11  2016-04-02          3  0.792600  0.546174  0.519287
12  2016-04-03          3  0.537685  0.235136  0.509063
13  2016-04-04          3  0.691400  0.028834  0.663123
14  2016-04-05          3  0.625309  0.734726  0.077092

例えば,シリアル番号ごとにVar1を規格化(平均0,標準偏差1)したい場合。インデックスをシリアル番号にして,シリアル番号でインデクシングする。

df.index = df['Serial_No']
for sn in set(df.index):
    mean = df.ix[sn, 'Var1'].mean()
    std = df.ix[sn, 'Var1'].std()
    df.ix[sn, 'Var1'] = (df.ix[sn, 'Var1'] - mean) / std

次のような書き方をすると,コピーされたDataFrameに対して処理をしてしまい,データの更新ができていないため注意。

df = pd.read_csv('sample.csv')
for sn in set(df['Serial_No']):
    mean = df[df['Serial_No'] == sn]['Var1'].mean()
    std = df[df['Serial_No'] == sn]['Var1'].std()
    df[df['Serial_No'] == sn]['Var1'] = (df[df['Serial_No'] == sn]['Var1'] - mean) / std # これはダメ

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?