Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

fill row with previous row from another column

I have a dataframe grouped by Client-Equipment, Date and Closing_Date. I show example:

Customer- Equipment Date Closing Date
Customer1 – Equipment A 2023-01-01 2023-01-05
Customer1 – Equipment A 2023-01-02 NaN
Customer1 – Equipment A 2023-01-03 NaN
Customer1 – Equipment A 2023-01-04 NaN
Customer1 – Equipment A 2023-01-05 NaN
Customer1 – Equipment A 2023-01-06 NaN
Customer2 – Equipment H 2023-01-01 2023-01-02
Customer2 – Equipment H 2023-01-02 NaN
Customer2 – Equipment H 2023-01-03 Nan

I need to fill in the Closing dates until the date is equal to the closing date. The expected result would be:

Customer- Equipment Date Closing Date
Customer1 – Equipment A 2023-01-01 2023-01-05
Customer1 – Equipment A 2023-01-02 2023-01-05
Customer1 – Equipment A 2023-01-03 2023-01-05
Customer1 – Equipment A 2023-01-04 2023-01-05
Customer1 – Equipment A 2023-01-05 2023-01-05
Customer1 – Equipment A 2023-01-06 NaN
Customer2 – Equipment H 2023-01-01 2023-01-02
Customer2 – Equipment H 2023-01-02 2023-01-02
Customer2 – Equipment H 2023-01-03 Nan

I’m trying codes like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df['test'] = df.groupby('Customer-Equipment').apply(
lambda x: x['Closing date'] if x['date'] <= x.at[row.index -1 ,'closing date'] else pd.NaT).fillna(method = 'ffill').reset_index(drop=True)

How could this be done in python?

>Solution :

If your dates are in increasing order, you could just groupby.ffill and mask with where:

s = df.groupby('Customer-Equipment')['Closing Date'].ffill()
df['Closing Date'] = s.where(s.ge(df['Date']))

Output:

        Customer-Equipment        Date Closing Date
0  Customer1 - Equipment A  2023-01-01   2023-01-05
1  Customer1 - Equipment A  2023-01-02   2023-01-05
2  Customer1 - Equipment A  2023-01-03   2023-01-05
3  Customer1 - Equipment A  2023-01-04   2023-01-05
4  Customer1 - Equipment A  2023-01-05   2023-01-05
5  Customer1 - Equipment A  2023-01-06          NaN
6  Customer2 - Equipment H  2023-01-01   2023-01-02
7  Customer2 - Equipment H  2023-01-02   2023-01-02
8  Customer2 - Equipment H  2023-01-03          Nan
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading