Questions tagged [ffill]

questions related to ffill method of pandas DataFrame or Series objects

Synonym for DataFrame.fillna() with method='ffill'.

Read more here.

See also:

46 questions
5
votes
2 answers

Forward fill only certain value

I have an array which represents object states, where 0 - object is off, and 1 - object is on. import pandas as pd import numpy as np s = [np.nan, 0, np.nan, np.nan, 1, np.nan, np.nan, 0, np.nan, 1, np.nan] df = pd.DataFrame(s, columns=["s"]) df …
crayxt
  • 2,367
  • 2
  • 12
  • 17
3
votes
1 answer

Is there a way to forward fill with ascending logic in pandas / numpy?

What is the most pandastic way to forward fill with ascending logic (without iterating over the rows)? input: import pandas as pd import numpy as np df = pd.DataFrame() df['test'] =…
bud fox
  • 335
  • 3
  • 16
3
votes
1 answer

Forward fill blocks of above values pandas

I'm interested in forward filling both single and multiple values in a column in pandas. With the following dataframe: import pandas as pd df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]]) df 0 1 2 0 1 2 3 1 4 NaN…
3
votes
1 answer

Filling empty cells in python (import dataset from excel)

I have a dataset where I want to fill the columns and rows in python as shown below: Dataset: | P | Q | |678|1420| |678|---| |609|---| |583|1260| |---|1260| |---|1261| |---|1262| |584|1263| |---|403| |---|---| Expected Result: | P | Q…
Dhruv
  • 43
  • 4
2
votes
1 answer

Generating monthly level data using ffill and bffill on multiplce columns of a log file

I have a log file in following format: Item Month_end_date old_price new_price row A 2022-03-31 25 30 1 A 2022-06-30 30 40 2 A 2022-08-31 40 45 3 B 2022-04-30 80 70 4 Here, its assumed that the price of the item A from the start of…
2
votes
2 answers

Pandas ffill and bfill in the same column - conditional

I have a column with chemistries and at some point I have the change of them. I want to ffill until the "changepoint" and bfill after it. I have several change points like this: date0 NaN date1 chem1 date2 NaN date3 NaN date4 change date5 …
2
votes
1 answer

Pandas DataFrame conditional forward filling based on first row values

I have the following DataFrame: import pandas as pd df = pd.DataFrame({ 'col1':['A',pd.NA,pd.NA,pd.NA,pd.NA, 'B', pd.NA, pd.NA], 'col2':[9.5, 6,24,8, 30, 7, 6, 8], }) print(df) Giving: col1 col2 0 A 9.5 1 6.0 2
McBuch
  • 43
  • 6
1
vote
1 answer

Python3 `pandas.DataFrame.ffill()` got an exception "TypeError: No matching signature found" caused by 'dtype'

Problem I used defaultdict to specify the dtype of each column. When I tried to ffill the dataframe, I got the following error. I think the error was caused by the dtype, but I have to specify it because my data is too large (1394265 rows × 300…
EvanHong
  • 11
  • 3
1
vote
1 answer

Python Pandas ffill or bffill with multiple condition

I have a dataset like below : condition=[None,None,None,'condtion1',None,None,None,'conditon2',None,None,None] add_val = [None,None,None,10,None,None,None,20,None,None,None] df=pd.DataFrame({'event_condition':condition,'add_col':add_val}) I want…
stat_man
  • 91
  • 5
1
vote
2 answers

How to forward fill by group and conditional on other columns in python

The original dataset is: Group Year Value A 1990 NaN A 1992 1 A 1995 NaN A 1997 NaN A 1998 NaN A 2001 NaN A 2002 1 B 1991 1 B 1992 NaN B 1995 NaN B 1998 NaN B 2001 1 B 2002 NaN I want to do forward fill by group…
leilei
  • 37
  • 5
1
vote
1 answer

Dataframe forward-fill till column-specific last valid index

How do I go from: [In]: df = pd.DataFrame({ 'col1': [100, np.nan, np.nan, 100, np.nan, np.nan], 'col2': [np.nan, 100, np.nan, np.nan, 100, np.nan] }) df [Out]: col1 col2 0 100 …
data-monkey
  • 1,535
  • 3
  • 15
  • 24
1
vote
3 answers

Pandas dataframe expand rows in specific times

I have a dataframe: df = T1 C1 01/01/2022 11:20 2 01/01/2022 15:40 8 01/01/2022 17:50 3 I want to expand it such that I will have the value in specific given times I will have a row for each round timestamp So…
Cranjis
  • 1,590
  • 8
  • 31
  • 64
1
vote
2 answers

Pandas ffill() to fill missing data

I am currently trying to fill blanks in a data frame that looks like the following: AL|ATFC|Year Latitude Longitude 0 AL011851 NaN NaN 1 NaN 28.0N 94.8W 2 NaN 28.0N 95.4W 3 NaN …
ezrix
  • 27
  • 4
1
vote
1 answer

Also ffill last value when resampling in pandas

I want to resample a dataframe and ffill values, see code below. However, the last value isn't forward filled. df=pd.DataFrame(index=pd.date_range(start='1/1/2022',periods=5,freq='h'),data=range(0,5)) print(df.resample('15T').ffill()) results in: …
Python404
  • 13
  • 3
1
vote
1 answer

I want to drag a column value which are in integer with respect to another column

Below is my data frame. df = pd.DataFrame({'vin':['aaa','aaa','aaa','bbb','bbb','bbb','ddd','eee','eee','fff'], 'c1':[35,'NA','NA','NA',4,'NA','NA','NA',56,406], 'c2':[35,'NA','NA','NA',43,'NA','NA','NA',67,77], …
jawed
  • 39
  • 4
1
2 3 4