0

I am working on a data cleaning project using Python 3 and a panda's dataframe. As a specific example: one of the changes I want to make is to a certain section of the dataframe that includes dates in a different format from the remained of the dataframe. I am able to make the changes I want to the format of the dates using a for loop, but I do not now how to insert these dates back into the dataframe.

Below is the code I have for parsing and rearranging the current datetime format (m/d/yy hh:mm)to the desired datetime format (yyyy/mm/dd hh:mm):

wrongDateFormat = df.iloc[807:1029]

for b in wrongDateFormat['datetime']:
        date = b.split()[0]
        time = b.split()[1]
        date = date.split('/')
        year = date[2]
        year = "20" + year
        month = date[0]
        if int(month) < 10:
            month = '0' + str(month)
        day = date[1]
        if int(day) < 10:
            day = '0' + str(day)

        b = year + '/' + month + '/' + day + ' ' + time
        print(b)

This code outputs:

2006/08/24 6:36
2006/08/23 11:59
2006/08/22 7:55
2006/08/21 2:25
2006/08/20 9:15
2006/08/19 8:16

Does anyone know how I can update the original dataframe with these new dates? Thanks in advance. It seems like the .set_value method used in this similar resolved question has depreciated since version 0.21.0.

Sawyer
  • 1
  • 2
  • 2
    Possible duplicate of [Update a dataframe in pandas while iterating row by row](https://stackoverflow.com/questions/23330654/update-a-dataframe-in-pandas-while-iterating-row-by-row) – 4d11 Jul 04 '18 at 17:39
  • 3
    Do you _have_ to use a loop? When you work with dataframes, a loop is usually not the right solution. – DYZ Jul 04 '18 at 17:40
  • Maybe a duplicate of these as well: https://stackoverflow.com/questions/38827835/how-to-update-a-dataframe-in-pandas-python; https://stackoverflow.com/questions/36531289/pandas-update-dataframe-row-with-new-value; https://stackoverflow.com/questions/18726497/how-to-update-existing-data-frame-in-pandas; https://stackoverflow.com/questions/24036911/how-to-update-values-in-a-specific-row-in-a-python-pandas-dataframe – Stuti Verma Jul 04 '18 at 18:18
  • Possible duplicate of [How to update a dataframe in Pandas Python](https://stackoverflow.com/questions/38827835/how-to-update-a-dataframe-in-pandas-python) – Stuti Verma Jul 04 '18 at 18:22

0 Answers0