-1

I open .xslx with pandas.

value date of pickup
ABC123 03.08.2022 11:24
ABC234 27.07.2022 15:45
ABC434 18.05.2022 02:35
ABVC122 28.07.2022 10:10

I need to keep only rows for previuos week. In my example today is 03.08.2022 wk number 31 I expect to see these rows:

value date of pickup
ABC234 27.07.2022 15:45
ABVC122 28.07.2022 10:10

I tried this: #remove week_num not in range df = df.drop([datetime.date(df['date of pickup']).isocalendar()[1] != weekly_num])

Do you have any ideas?

Thank you for your support Angelo

1 Answers1

1

You can use something like this

import pandas as pd
import datetime
df = pd.DataFrame({'value':['a', 'b', 'c', 'd'], 'date':pd.date_range("2022-07-01", "2022-08-03", periods=4)})
df[(df['date'] <= pd.to_datetime("today") - datetime.timedelta(weeks=1)) & (df['date'] >= pd.to_datetime("today") - datetime.timedelta(weeks=2))]

output

value   date
c       2022-07-23

Next time if you can provide the dataframe creation code, it will be easier for us to answer your question.

Osca
  • 1,588
  • 2
  • 20
  • 41
  • Hello Osca, thank you for your reply and sorry for missing code. Here is my code: ---------------- import pandas as pd import datetime my_date = datetime.date.today() # if date is 01/01/2018 year, week_num, day_of_week = my_date.isocalendar() weekly_num = week_num -1 df = pd.read_csv('C:\_est\test.csv', sep=";", encoding='windows-1252', low_memory=False) df = df.drop([datetime.date(df['date of pickup']).isocalendar()[1] != weekly_num]) df.to_csv('C:\_est\test2.csv', sep=';', index=False, encoding='windows-1252') – Angelo Malfitano Aug 03 '22 at 10:45
  • 1
    that's not what I meant by sharing the dataframe creation code. Anyway, my code already demonstrates how to do it. You can use it on your data. – Osca Aug 03 '22 at 10:54