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
#11
StringIO as i used is not needed when read from a file.
It make the string i copied from you post act like a file-like object in memory..
import pandas as pd

df = pd.read_csv("data.csv", names=["Date", "Time", "Comment"])
df['DateTime'] = pd.to_datetime(df["Date"] + df["Time"])
mask = (df['DateTime'] >= '2022-03-08 16:15:00')
df_new = df.loc[mask]
#df_new = df_new.drop(columns=['DateTime'])
df_new = df_new.drop(columns=['Date', 'Time'])
print(df_new)
Output:
Comment DateTime 6 String Value 2022-03-08 16:15:00 7 String Value 2022-03-08 16:15:00 8 String Value 2022-03-08 16:15:00 9 String Value 2022-03-08 16:15:00 10 String Value 2022-03-08 16:17:00 11 String Value 2022-03-08 16:17:00
Line 7 uncomment:
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
#12
I am guessing this is a typo:
mask = (df['DateTime'] > '2022-03-08 16:06:00') & (df['DateTime'] >= '2022-03-08 16:15:00')
Any datetime > '2022-03-08 16:06:00' is also >= '2022-03-08 16:15:00'.
Maybe it was going to be an example of a start and end datetime.
mask = (df['DateTime'] > '2022-03-08 16:06:00') & (df['DateTime'] <= '2022-03-08 16:15:00')
Reply
#13
(Mar-18-2022, 05:52 PM)deanhystad Wrote: Maybe it was going to be an example of a start and end datetime.
Yes that was the idea,thx for the correction.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find (each) element from a list in a file tester_V 3 289 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 255 Sep-27-2022, 01:38 PM
Last Post: buran
  Graphic line plot with matplotlib, text file in pytho khadija 2 517 Aug-15-2022, 12:00 PM
Last Post: khadija
  read a text file, find all integers, append to list oldtrafford 12 1,081 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  what will be the best way to find data in txt file? korenron 2 427 Jul-25-2022, 10:03 AM
Last Post: korenron
  Delete multiple lines from txt file Lky 6 718 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Delete empty text files [SOLVED] AlphaInc 5 598 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  find some word in text list file and a bit change to them RolanRoll 3 549 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  Modify values in XML file by data from text file (without parsing) Paqqno 2 638 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Skipping line in text without Restarting Loop IdMineThat 4 596 Apr-05-2022, 04:23 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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