Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas dataframe.ffill()
函數用於填充 DataFrame 中的缺失值。 “填充”代表“向前填充”,並將向前傳播最後一個有效觀察值。
用法: DataFrame.ffill(axis=None, inplace=False, limit=None, downcast=None)
參數:
axis:{0,索引1,欄}
inplace:如果為True,則填寫。注意:這將修改此對象的任何其他視圖(例如,DataFrame中列的no-copy切片)。
limit:如果指定了method,則這是要向前/向後填充的連續NaN值的最大數量。換句話說,如果存在連續的NaN數量大於此數量的缺口,它將僅被部分填充。如果未指定method,則這是將填寫NaN的整個軸上的最大條目數。如果不為None,則必須大於0。
Downcast:item-> dtype決定是否向下轉換的內容,或字符串“ infer”將嘗試向下轉換為適當的相等類型(例如,如果可能,將float64轉換為int64)
返回:fill:DataFrame
範例1:采用ffill()
函數沿索引軸填充缺失值。
注意:什麽時候ffill()
在索引上應用,然後根據上一行中的相應值填充所有缺失值。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df=pd.DataFrame({"A":[5,3,None,4],
"B":[None,2,4,3],
"C":[4,3,8,5],
"D":[5,4,2,None]})
# Print the dataframe
df
讓我們在索引軸上填充缺失值
# applying ffill() method to fill the missing values
df.ffill(axis = 0)
輸出:
請注意,第一行中的值仍然NaN
值,因為它上方沒有行可以傳播非NA值。
範例2:采用ffill()
函數沿列軸填充缺失值。
注意:當在列軸上應用填充時,缺少的值將由同一行中前一列中的值填充。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df=pd.DataFrame({"A":[5,3,None,4],
"B":[None,2,4,3],
"C":[4,3,8,5],
"D":[5,4,2,None]})
# Print the dataframe
df
讓我們在列軸上填充缺失值
# applying ffill() method to fill the missing values
df.ffill(axis = 1)
輸出:
請注意,第一欄中的vlaues是NaN
值,因為沒有剩餘單元格,因此無法使用沿列軸的先前單元格值來填充此單元格。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.str.len()用法及代碼示例
- Python Pandas.factorize()用法及代碼示例
- Python Pandas TimedeltaIndex.name用法及代碼示例
- Python Pandas dataframe.ne()用法及代碼示例
- Python Pandas Series.between()用法及代碼示例
- Python Pandas DataFrame.where()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas.pivot_table()用法及代碼示例
- Python Pandas Series.mod()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas Dataframe.iat[ ]用法及代碼示例
- Python Pandas.pivot()用法及代碼示例
- Python Pandas dataframe.mul()用法及代碼示例
- Python Pandas.melt()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas dataframe.ffill()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。