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

How do I get a previous date from iterrows() function (Python and Pandas)

I have a dataframe that I’m going through as follows:

  for index, row in my_dataframe.iterrows():
        cursor.execute(""" UPDATE MY_TABLE SET COLUMN1 = %s, 
        COLUMN2 = %s WHERE TO_TIMESTAMP(DATE_COLUMN, 'YYYY-MM-DD') = %s AND COLUMN1 IS NULL""",
                                        (row['VALUE_A'],
                                         row['VALUE_B'],
                                         row['DATE_C']
                                         ))

What I need is to apply something like:

row['DATE_C'] -1 day. 

How can I do that? How can we do row manipulations in python / pandas data frame.

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

Now, I know if we took out the row[‘…’] syntax and this was basic sql, we could just use the – interval ‘1’ day syntax to get this done. But I need to use this loop to achieve my goal.
Thank you.

>Solution :

You can use pandas.Timedelta

pd.to_datetime(row['DATE_C']) - pd.Timedelta(days=1)
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