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

Insert a row after a date in a dataframe

I have a Dataframe like :

    date                   col1    col2
0   2022-10-07 04:00:00    x       x1
1   2022-10-08 04:00:00    y       x2

I need to update a row (as dictionary) in a specific date if exist, and if it does not exist, insert the row next to the closest date.

For this new given date 2022-10-07 05:00:00 (one hour later) and dic {col1:z} I would like to get :

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

    date                   col1   col2
0   2022-10-07 04:00:00    x      x1
1   2022-10-07 05:00:00    z      x1
2   2022-10-08 04:00:00    y      x2

Currently I am doing this:

def write(date,dic):
    m = df['date'] == date
    if m.any():
        df.loc[df['date'] == date, list(dic.keys())] =  list(dic.values()) 
    else:
        df.loc[len(df)] = {**dic, **{'date':date}}

Which means that if I can’t find the date, I just add the row to the end of the df, but I want to insert it right after the previous date.

(Please also see that since when I insert/update I only have col1, so col2 value will be copied from previous row somehow with ffill)

>Solution :

You can set the date as index and update like:

df1 = df.set_index('date')
df1.loc[new_date, dic.keys()] = dic.values()
df = df1.sort_index().reset_index().ffill()

It will insert new date if it doesn’t exist. If it exists it will update the record at that index.

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