【Pandas】diffメソッドで行・列の差分を計算

Pythonモジュール「Pandas」のdiffメソッドで行・列の差分を計算する件についてまとめました。

## 【diff】行同士の差分を計算

pandasでは、diffメソッドで行同士の差分を計算できます。
引数で○行前後と差分を取るか指定できます。

# -*- coding: utf-8 -*-
import pandas as pd

# データ(辞書型)
data_dict = {
    'time'  : [1, 2, 3, 4, 5],
    'temp1' : [25, 26, 29, 28, 27],
    'temp2' : [27, 29, 34, 32, 31]
    }

# データフレームの初期化
df = pd.DataFrame(data_dict)
print(df)
"""
   time  temp1  temp2
0     1     25     27
1     2     26     29
2     3     29     34
3     4     28     32
4     5     27     31
"""

# 1行前の値との差分を計算
print(df.diff())
"""
   time  temp1  temp2
0   NaN    NaN    NaN
1   1.0    1.0    2.0
2   1.0    3.0    5.0
3   1.0   -1.0   -2.0
4   1.0   -1.0   -1.0
"""

# 2行前の値との差分を計算
print(df.diff(2))
"""
   time  temp1  temp2
0   NaN    NaN    NaN
1   NaN    NaN    NaN
2   2.0    4.0    7.0
3   2.0    2.0    3.0
4   2.0   -2.0   -3.0
"""

# 2行後の値との差分を計算
print(df.diff(-2))
"""
   time  temp1  temp2
0  -2.0   -4.0   -7.0
1  -2.0   -2.0   -3.0
2  -2.0    2.0    3.0
3   NaN    NaN    NaN
4   NaN    NaN    NaN
"""

特定の列のみ(Series)で差分を求めることもできます。

# -*- coding: utf-8 -*-
import pandas as pd

# データ(辞書型)
data_dict = {
    'time'  : [1, 2, 3, 4, 5],
    'temp1' : [25, 26, 29, 28, 27],
    'temp2' : [27, 29, 34, 32, 31]
    }

# データフレームの初期化
df = pd.DataFrame(data_dict)
print(df)
"""
   time  temp1  temp2
0     1     25     27
1     2     26     29
2     3     29     34
3     4     28     32
4     5     27     31
"""

# 1列前の値との差分を計算
df["diff_time"] = df["time"].diff()

# 1行後の値との差分を計算
df["diff_temp1"] = df["temp1"].diff(-1)
print(df)
"""
   time  temp1  temp2  diff_time  diff_temp1
0     1     25     27        NaN        -1.0
1     2     26     29        1.0        -3.0
2     3     29     34        1.0         1.0
3     4     28     32        1.0         1.0
4     5     27     31        1.0         NaN
"""

## 【diff】列同士の差分を計算

diffメソッドの引数axisで「1」を指定すると列士の差分を計算できます。
引数で○列前後と差分を取るか指定できます。

# -*- coding: utf-8 -*-
import pandas as pd

# データ(辞書型)
data_dict = {
    'time'  : [1, 2, 3, 4, 5],
    'temp1' : [25, 26, 29, 28, 27],
    'temp2' : [27, 29, 34, 32, 31]
    }

# データフレームの初期化
df = pd.DataFrame(data_dict)
print(df)
"""
   time  temp1  temp2
0     1     25     27
1     2     26     29
2     3     29     34
3     4     28     32
4     5     27     31
"""

# 1列前の値との差分を計算
print(df.diff(axis=1))
"""
   time  temp1  temp2
0   NaN   24.0    2.0
1   NaN   24.0    3.0
2   NaN   26.0    5.0
3   NaN   24.0    4.0
4   NaN   22.0    4.0
"""

# 2列前の値との差分を計算
print(df.diff(2, axis=1))
"""
   time  temp1  temp2
0   NaN    NaN   26.0
1   NaN    NaN   27.0
2   NaN    NaN   31.0
3   NaN    NaN   28.0
4   NaN    NaN   26.0
"""

# 2列後の値との差分を計算
print(df.diff(-2, axis=1))
"""
   time  temp1  temp2
0 -26.0    NaN    NaN
1 -27.0    NaN    NaN
2 -31.0    NaN    NaN
3 -28.0    NaN    NaN
4 -26.0    NaN    NaN
"""

## 【pct_change】行・列同士の変化率を計算

pct_changeメソッドで○行・列前後との変化率を計算できます。

# -*- coding: utf-8 -*-
import pandas as pd

# データ(辞書型)
data_dict = {
    'time'  : [1, 2, 3, 4, 5],
    'temp1' : [25, 26, 29, 28, 27],
    'temp2' : [27, 29, 34, 32, 31]
    }

# データフレームの初期化
df = pd.DataFrame(data_dict)
print(df)
"""
   time  temp1  temp2
0     1     25     27
1     2     26     29
2     3     29     34
3     4     28     32
4     5     27     31
"""

# 1行前との変化率を計算
print(df.pct_change())
"""
       time     temp1     temp2
0       NaN       NaN       NaN
1  1.000000  0.040000  0.074074
2  0.500000  0.115385  0.172414
3  0.333333 -0.034483 -0.058824
4  0.250000 -0.035714 -0.031250
"""

# 1行前との変化率を計算(time列のみ)
df["pct_change_time"] = df["time"].pct_change()

# 1行前との変化率を計算(temp1列のみ)
df["pct_change_temp1"] = df["temp1"].pct_change()

print(df)
"""
   time  temp1  temp2  pct_change_time  pct_change_temp1
0     1     25     27              NaN               NaN
1     2     26     29         1.000000          0.040000
2     3     29     34         0.500000          0.115385
3     4     28     32         0.333333         -0.034483
4     5     27     31         0.250000         -0.035714
"""
関連記事
1 【Pandas入門】データ分析のサンプル集
2 【Python入門】サンプル集・使い方
Python
スポンサーリンク

コメント