Pandas DataFrame interpolate() 方法
示例
使用上一行和下一行的數字替換 NULL 值
在此示例中,我們使用名為 data.csv 的 .csv 檔案
import pandas as pd
df = pd.read_csv('data.csv')
newdf = df.interpolate(method='linear')
自己動手試一試 »
定義和用法
interpolate()
方法根據指定的方法替換 NULL 值。
語法
dataframe.interpolate(method, axis, inplace, limit, limit_direction, limit_area, downcast, kwargs)
引數
引數是 關鍵字引數。
引數 | 值 | 描述 |
---|---|---|
method | 'linear' |
可選,預設為 'linear'。指定替換 NULL 值時使用的方法 |
axis | 0 |
可選,預設 0。用於填充 NULL 值的軸。 |
inplace | True |
可選,預設 False。如果為 True:在當前 DataFrame 中進行替換。如果為 False:返回一個已進行替換的副本。 |
limit | 數字None |
可選,預設為 None。指定要填充的最大 NULL 值數量(如果指定了 method) |
limit_direction | 'forward' |
可選,預設為 'forward'(如果 method 是 backfill 或 bfill,則預設的 limit_direction 為 'backward')。指定填充的方向。 |
limit_area | 無 |
可選,預設為 None。指定填充的限制。 None - 無限制 'inside' - 僅填充有效值內部的 NULL 值 'outside' - 僅填充有效值外部的 NULL 值 |
downcast | 字典None |
可選,一個用於填充特定資料型別的值的字典。 |
返回值
一個帶有結果的DataFrame,如果 inplace 引數設定為 True,則為 None。