Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forward fill specific columns in pandas dataframe

Tags:

python

pandas

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.

like image 779
azuric Avatar asked Nov 19 '14 08:11

azuric


People also ask

How do I fill a particular column in pandas DataFrame?

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.

How do I fill a specific value in pandas?

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.

What is Ffill and bfill in pandas?

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.

What is Ffill Fillna?

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.


2 Answers

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 
like image 169
Hennep Avatar answered Sep 18 '22 08:09

Hennep


for col in ['X', 'Y']:     df[col] = df[col].ffill() 
like image 20
Woody Pride Avatar answered Sep 18 '22 08:09

Woody Pride