Pandas DataFrame DataFrame.interpolate() 関数
-
pandas.DataFrame.interpolate()の構文 -
コード例:
DataFrame.interpolate()メソッドを用いてDataFrame内のすべてのNaN値を補間する -
コード例:
DataFrame.interpolate()メソッドmethodパラメータを持つメソッド -
コード例:Pandas
DataFrame.interpolate()メソッドaxisパラメータを用いてrow軸に沿って補間する -
コード例:
DataFrame.interpolate()メソッド withlimitパラメータ -
コード例:
DataFrame.interpolate()メソッド withlimit_directionパラメータ -
時系列データを
DataFrame.interpolate()メソッドで補間する
Python Pandas DataFrame.interpolate() 関数は補間手法を用いて DataFrame 内の NaN の値を補間します。
pandas.DataFrame.interpolate() の構文
DataFrame.interpolate(
method="linear",
axis=0,
limit=None,
inplace=False,
limit_direction="forward",
limit_area=None,
downcast=None,
**kwargs
)
パラメータ
method |
linear、time、index、values、nearest、zero、slinear、quadratic、cubic、barycentric、krogh、polynomial、spline、piecewise_polynomial、from_derivatives、pchip、akima、None. NaN の値を補間するためのメソッド。 |
axis |
行(axis=0)または列(axis=1)に沿って欠損値を補間します。 |
limit |
整数。補間される連続した NaN の最大数。 |
inplace |
論理値。True ならば、呼び出し元の DataFrame を変更します。 |
limit_direction |
forward、backward、または both。limit が指定された場合、NaNs に沿った方向は補間されます。 |
limit_area |
None、inside、outside。limit が指定された場合の補間の制限 |
downcast |
辞書。データ型のダウンキャストを指定します。 |
**kwargs |
補間関数のキーワード引数。 |
戻り値
inplace が True の場合、与えられた method を用いてすべての NaN 値を補間する DataFrame を作成し、そうでない場合は None を作成します。
コード例:DataFrame.interpolate() メソッドを用いて DataFrame 内のすべての NaN 値を補間する
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3, None, 3],
'Y': [4, None, 8, None, 3]})
print("DataFrame:")
print(df)
filled_df = df.interpolate()
print("Interploated DataFrame:")
print(filled_df)
出力:
DataFrame:
X Y
0 1.0 4.0
1 2.0 NaN
2 3.0 8.0
3 NaN NaN
4 3.0 3.0
Interploated DataFrame:
X Y
0 1.0 4.0
1 2.0 6.0
2 3.0 8.0
3 3.0 5.5
4 3.0 3.0
これは DataFrame のすべての NaN 値を linear 補間手法を用いて補間します。
このメソッドは pandas.DataFrame.fillna() に比べてよりインテリジェントです。
コード例:DataFrame.interpolate() メソッド method パラメータを持つメソッド
また、DataFrame.interpolate() 関数の method パラメータの値を設定することで、DataFrame の NaN 値をさまざまな補間手法で補間することもできます。
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3, None, 3],
'Y': [4, None, 8, None, 3]})
print("DataFrame:")
print(df)
filled_df = df.interpolate(method='polynomial', order=2)
print("Interploated DataFrame:")
print(filled_df)
出力:
DataFrame:
X Y
0 1.0 4.0
1 2.0 NaN
2 3.0 8.0
3 NaN NaN
4 3.0 3.0
Interploated DataFrame:
X Y
0 1.000000 4.000
1 2.000000 7.125
2 3.000000 8.000
3 3.368421 6.625
4 3.000000 3.000
このメソッドは DataFrame の NaN のすべての値を 2 次の polynomial 補間手法を用いて補間します。
ここで、order=2 は polynomial 関数のキーワード引数です。
コード例:Pandas DataFrame.interpolate() メソッド axis パラメータを用いて row 軸に沿って補間する
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3, None, 3],
'Y': [4, None, 8, None, 3]})
print("DataFrame:")
print(df)
filled_df = df.interpolate(axis=1)
print("Interploated DataFrame:")
print(filled_df)
出力:
DataFrame:
X Y
0 1.0 4.0
1 2.0 NaN
2 3.0 8.0
3 NaN NaN
4 3.0 3.0
Interploated DataFrame:
X Y
0 1.0 4.0
1 2.0 2.0
2 3.0 8.0
3 NaN NaN
4 3.0 3.0
ここでは、行軸に沿って NaN の値を補間するために axis=1 を設定しています。2 行目では、2 行目に沿った線形補間で NaN の値を置き換えています。
しかし、4 行目では、4 行目の値はいずれも NaN であるため、補間後も NaN の値が残っています。
コード例:DataFrame.interpolate() メソッド with limit パラメータ
DataFrame.interpolate() メソッドの limit パラメータは、このメソッドで塗りつぶされる NaN 値の最大連続数を制限します。
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3, None, 3],
'Y': [4, None, None, None, 3]})
print("DataFrame:")
print(df)
filled_df = df.interpolate( limit = 1)
print("Interploated DataFrame:")
print(filled_df)
出力:
DataFrame:
X Y
0 1.0 4.0
1 2.0 NaN
2 3.0 NaN
3 NaN NaN
4 3.0 3.0
Interploated DataFrame:
X Y
0 1.0 4.00
1 2.0 3.75
2 3.0 NaN
3 3.0 NaN
4 3.0 3.00
ここでは、ある列で上から順に NaN が塗りつぶされると、同じ列の次の連続する NaN の値は変更されません。
コード例:DataFrame.interpolate() メソッド with limit_direction パラメータ
DataFrame.interpolate() メソッドの limit-direction パラメータは、値を補間する特定の axis に沿った方向を制御します。
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3, None, 3],
'Y': [4, None, None, None, 3]})
print("DataFrame:")
print(df)
filled_df = df.interpolate(limit_direction ='backward', limit = 1)
print("Interploated DataFrame:")
print(filled_df)
出力:
DataFrame:
X Y
0 1.0 4.0
1 2.0 NaN
2 3.0 NaN
3 NaN NaN
4 3.0 3.0
Interploated DataFrame:
X Y
0 1.0 4.00
1 2.0 NaN
2 3.0 NaN
3 3.0 3.25
4 3.0 3.00
ここでは、一旦 NaN が下から順に列に埋められると、同じ列の次の連続する NaN の値は変更されません。
時系列データを DataFrame.interpolate() メソッドで補間する
import pandas as pd
dates=['April-10', 'April-11', 'April-12', 'April-13']
fruits=['Apple', 'Papaya', 'Banana', 'Mango']
prices=[3, None, 2, 4]
df = pd.DataFrame({'Date':dates ,
'Fruit':fruits ,
'Price': prices})
print(df)
df.interpolate(inplace=True)
print("Interploated DataFrame:")
print(df)
出力:
Date Fruit Price
0 April-10 Apple 3.0
1 April-11 Papaya NaN
2 April-12 Banana 2.0
3 April-13 Mango 4.0
Interploated DataFrame:
Date Fruit Price
0 April-10 Apple 3.0
1 April-11 Papaya 2.5
2 April-12 Banana 2.0
3 April-13 Mango 4.0
inplace=True のため、関数 interpolate() を呼び出した後に元の DataFrame が変更されます。
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn