Python|熊猫 dataframe.ffill()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.ffill()函数用于填充数据框中的缺失值。 “填充”代表“前向填充”,并将向前传播最后一个有效观察。
Syntax: DataFrame.ffill(axis=None, inplace=False, limit=None, downcast=None)
Parameters:
axis : {0, index 1, column}
inplace : If True, fill in place. Note: this will modify any other views on this object, (e.g. a no-copy slice for a column in a DataFrame).
limit : If method is specified, this is the maximum number of consecutive NaN values to forward/backward fill. In other words, if there is a gap with more than this number of consecutive NaNs, it will only be partially filled. If method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. Must be greater than 0 if not None.
Downcast: a dict of item->dtype of what to downcast if possible, or the string ‘infer’ which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible)
Returns : filled : DataFrame
示例 #1:使用 ffill()函数沿索引轴填充缺失值。
注意:当 ffill() 应用于索引时,任何缺失值都将根据前一行中的相应值进行填充。
Python3
# 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
Python3
# applying ffill() method to fill the missing values
df.ffill(axis = 0)
Python3
# 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
Python3
# applying ffill() method to fill the missing values
df.ffill(axis = 1)
让我们在索引轴上填充缺失值
Python3
# applying ffill() method to fill the missing values
df.ffill(axis = 0)
输出 :
请注意,第一行中的值仍然是 NaN 值,因为它上面没有可以传播非 NA 值的行。示例 #2:使用 ffill()函数沿列轴填充缺失值。
注意:当跨列轴应用 ffill 时,缺失值将由同一行中前一列中的值填充。
Python3
# 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
让我们在列轴上填充缺失值
Python3
# applying ffill() method to fill the missing values
df.ffill(axis = 1)
输出 :
请注意,第一列中的值是 NaN 值,因为它没有剩余单元格,因此无法使用沿列轴的前一个单元格值填充该单元格。