May-09-2022, 08:19 PM
Well i spoke to fast, it was working fine in my test script, but now that i implemented the changes above to my actual script, im getting the datetime column at the end even though i have the df.drop before writing the new csv file..
How can that happen? What is wrong with this now? The replacing works, the datetime filter works, but when its written to the new text file, the temp column df['DT'] is appended to the end of the 3 original columns.
Unless i opened an old text file this morning when i checked, i thought this was already working..
How can that happen? What is wrong with this now? The replacing works, the datetime filter works, but when its written to the new text file, the temp column df['DT'] is appended to the end of the 3 original columns.
Unless i opened an old text file this morning when i checked, i thought this was already working..
# THIS OPENS THE NEWLY CLEAN DATA IN ORDER TO REMOVE OLD RECORDS df = pd.read_csv("Dates.txt", usecols=range(3), names=["Date", "Time", "Comment"]) df['Comment'] = [re.sub(r'(?:^|\W)Someone Else:(?:$|\W)','Dan: ', str(x)) for x in df['Comment']] df['Comment'] = [re.sub(r'(?:^|\W)Elizabeth:(?:$|\W)','Ross: ', str(x)) for x in df['Comment']] # THIS CONVERTS THE DATE COLUMN INTO A DATETIME FORMAT df['DT'] = pd.to_datetime(df['Date'] + ' ' + df['Time']) # HERE YOU NEED TO PROVIDE THE DATE YOU WANT TO KEEP GOING FORWARD mask = (df['DT'] > '2022-05-02 9:32 AM') # THIS RETURNS ALL ROWS GREATER THAN THE DATE PROVIDED ABOVE df = df.loc[mask] # DROP TEMP COLUMN BEFORE WRITING CSV df['DT'].drop # THIS IS THE FILTERED DATA RESULTS TO IMPORT INTO EXCEL df.to_csv(r'C:\Users\mynewfile.txt', header=None, index=None, mode='a')