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
#9
(Mar-17-2022, 01:32 AM)deanhystad Wrote: Is it better to make a column of strings and then convert to datetime? I assumed that is what happens here:
I could have done it one step,which would have saved me a line.
Looking at it again so is your post #8 fine,just grab a datetime object and use it filtering is good👍
I was more thinking about post #2 where you did use datetime from standard library.

So update make code to use one step and as a example doing it the other way around dropping DateTime if want the original split in Date and Time.
import pandas as pd
from io import StringIO

data = """\
3/8/22, 4:06 PM, String Value
3/8/22, 4:12 PM, String Value
3/8/22, 4:12 PM, String Value
3/8/22, 4:13 PM, String Value
3/8/22, 4:14 PM, String Value
3/8/22, 4:14 PM, String Value
3/8/22, 4:15 PM, String Value
3/8/22, 4:15 PM, String Value
3/8/22, 4:15 PM, String Value
3/8/22, 4:15 PM, String Value
3/8/22, 4:17 PM, String Value
3/8/22, 4:17 PM, String Value"""

df = pd.read_csv(StringIO(data), sep=",", names=["Date", "Time", "Comment"])
df['DateTime'] = pd.to_datetime(df["Date"] + df["Time"])
mask = (df['DateTime'] > '2022-03-08 16:06:00') & (df['DateTime'] >= '2022-03-08 16:15:00')
df_new = df.loc[mask]
df_new = df_new.drop(columns=['DateTime'])
print(df_new)
Output:
Date Time Comment 6 3/8/22 4:15 PM String Value 7 3/8/22 4:15 PM String Value 8 3/8/22 4:15 PM String Value 9 3/8/22 4:15 PM String Value 10 3/8/22 4:17 PM String Value 11 3/8/22 4:17 PM String Value
Reply


Messages In This Thread
RE: Find and delete above a certain line in text file - by snippsat - Mar-17-2022, 08:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,610 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,631 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Cannot find py credentials file standenman 5 1,692 Feb-25-2023, 08:30 PM
Last Post: Jeff900
  selenium can't find a file in my desk ? SouAmego22 0 754 Feb-14-2023, 03:21 PM
Last Post: SouAmego22
  Pypdf2 will not find text standenman 2 955 Feb-03-2023, 10:52 PM
Last Post: standenman
  Getting last line of each line occurrence in a file tester_V 1 885 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 1,151 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Find (each) element from a list in a file tester_V 3 1,247 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,404 Sep-27-2022, 01:38 PM
Last Post: buran
  Graphic line plot with matplotlib, text file in pytho khadija 2 1,415 Aug-15-2022, 12:00 PM
Last Post: khadija

Forum Jump:

User Panel Messages

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