Home > Enterprise >  Select rows in a data frame based on the date range
Select rows in a data frame based on the date range

Time:02-25

I have a list of dictionaries:

mylist=
[{'Date': '10/2/2021', 'ID': 11773, 'Receiver': 'Mike'},
{'Date': '10/2/2021', 'ID': 15673, 'Receiver': 'Jane'},
{'Date': '10/3/2021', 'ID': 11773, 'Receiver': 'Mike'},
... 
{'Date': '12/25/2021', 'ID': 34653, 'Receiver': 'Jack'}]

I want to select the rows within a date range, for example from 10/3/2021 to 11/3/2021. I tried the following steps:

dfmylist = pd.DataFrame(mylist)
dfmylistnew = (dfmylist['Date'] > '10/3/2021') & (dfmylist['Date'] <= '11/3/2021')

I converted my list to a data frame and then select the date range. However, the dfmylistnew data frame doesn't show up properly. What did I miss?

The output of dfmylistnew is:

0    False
1    False
2    False
3    False
4    False
Name: Date, dtype: bool

CodePudding user response:

You missed to convert the "Date" to datetime type:

import pandas as pd

mylist=[{'Date': '10/2/2021', 'ID': 11773, 'Receiver': 'Mike'},
{'Date': '10/2/2021', 'ID': 15673, 'Receiver': 'Jane'},
{'Date': '10/3/2021', 'ID': 11773, 'Receiver': 'Mike'},
{'Date': '12/25/2021', 'ID': 34653, 'Receiver': 'Jack'}]

dfmylist = pd.DataFrame(mylist)

dfmylist['Date'] = pd.to_datetime(dfmylist['Date']) # you missed this line

dfmylistnew = (dfmylist['Date'] > '10/2/2021') & (dfmylist['Date'] <= '11/3/2021')

dfmylist.loc[dfmylistnew]

CodePudding user response:

One option you have is to change the Date column to the index of the Dataframe, once that is set to the index python will recognize it as a date field and you can use a df.loc to find the data between specified dates.

df = pd.DataFrame(mylist)
df.set_index('Date')
df.loc['10/3/2021':'11/3/2021']

CodePudding user response:

You can try this:

dfmylistnew = dfmylist[list(dfmylist['Date'] > '10/3/2021') and list(dfmylist['Date']<='11/3/2021')]
  • Related