Python Forum
Find and delete above a certain line in text file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Find and delete above a certain line in text file (/thread-36661.html)

Pages: 1 2


RE: Find and delete above a certain line in text file - snippsat - Mar-17-2022

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



RE: Find and delete above a certain line in text file - deanhystad - Mar-18-2022

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')



RE: Find and delete above a certain line in text file - snippsat - Mar-18-2022

(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.