If I have a dataframe with multiple columns ['x', 'y', 'z']
, how do I forward fill only one column 'x'
? Or a group of columns ['x','y']
?
I only know how to do it by axis.
ffill() function is used to fill the missing value in the dataframe. 'ffill' stands for 'forward fill' and will propagate last valid observation forward. inplace : If True, fill in place.
Pandas DataFrame fillna() MethodThe fillna() method replaces the NULL values with a specified value. The fillna() method returns a new DataFrame object unless the inplace parameter is set to True , in that case the fillna() method does the replacing in the original DataFrame instead.
bfill() is used to backward fill the missing values in the dataset. It will backward fill the NaN values that are present in the pandas dataframe. ffill() function is used forward fill the missing value in the dataframe.
method='ffill': Ffill or forward-fill propagates the last observed non-null value forward until another non-null value is encountered. method='bfill': Bfill or backward-fill propagates the first observed non-null value backward until another non-null value is met.
tl;dr:
cols = ['X', 'Y'] df.loc[:,cols] = df.loc[:,cols].ffill()
And I have also added a self containing example:
>>> import pandas as pd >>> import numpy as np >>> >>> ## create dataframe ... ts1 = [0, 1, np.nan, np.nan, np.nan, np.nan] >>> ts2 = [0, 2, np.nan, 3, np.nan, np.nan] >>> d = {'X': ts1, 'Y': ts2, 'Z': ts2} >>> df = pd.DataFrame(data=d) >>> print(df.head()) X Y Z 0 0 0 0 1 1 2 2 2 NaN NaN NaN 3 NaN 3 3 4 NaN NaN NaN >>> >>> ## apply forward fill ... cols = ['X', 'Y'] >>> df.loc[:,cols] = df.loc[:,cols].ffill() >>> print(df.head()) X Y Z 0 0 0 0 1 1 2 2 2 1 2 NaN 3 1 3 3 4 1 3 NaN
for col in ['X', 'Y']: df[col] = df[col].ffill()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With