Python Forum
Find and delete above a certain line in text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find and delete above a certain line in text file
#10
@snippsat can i ask why have the mask using 2 dates?

mask = (df['DateTime'] > '2022-03-08 16:06:00') & (df['DateTime'] >= '2022-03-08 16:15:00')
Ill be trying both suggestions above shortly after my meeting, so thank you again for the suggestions..
Also just to be clear, i can add "columns" by adding a dataframe after reading in the file like below? and if i want 2 columns, i can just define it below this one and it will add both to the end of the file columns correct?

df["DateTime"] = df["Date"] + df["Time"]

So i implemented the suggestions above.. but getting an error:

import pandas as pd
from io import StringIO

df = pd.read_csv(StringIO("Dates.txt"), sep=",", names=["Date", "Time", "Comment"])

df['Date'] = pd.to_datetime(df['Date'])
df['DateTime'] = pd.to_datetime(df["Date"] + df["Time"])

mask = (df['DateTime'] > '2022-03-08 15:18:00')

df_new = df.loc[mask]
df_new = df_new.drop(columns=['DateTime'])

print(df_new)
Error:
ParserError: Too many columns specified: expected 3 and found 1

Figured it out.. because i had left this line in place, it was causing the issue

df['Date'] = pd.to_datetime(df['Date'])
removed it and runs and works like a charm, thank you so much for all the suggestions and help.
Reply


Messages In This Thread
RE: Find and delete above a certain line in text file - by cubangt - Mar-17-2022, 01:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,811 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  Delete file with read-only permission, but write permission to parent folder cubei 6 25,498 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 3,036 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 15,529 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Cannot find py credentials file standenman 5 3,288 Feb-25-2023, 08:30 PM
Last Post: Jeff900
  selenium can't find a file in my desk ? SouAmego22 0 1,261 Feb-14-2023, 03:21 PM
Last Post: SouAmego22
  Pypdf2 will not find text standenman 2 1,827 Feb-03-2023, 10:52 PM
Last Post: standenman
  Getting last line of each line occurrence in a file tester_V 1 1,496 Jan-31-2023, 09:29 PM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 2,027 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Find (each) element from a list in a file tester_V 3 2,201 Nov-15-2022, 08:40 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020