4 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonPandas: How to Select Rows Between Two Dates

Pandas: How to Select Rows Between Two Dates

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following syntax to select rows between two specific dates in a pandas DataFrame:

df[df.date.between('2022-01-02', '2022-01-06')]

This particular example selects all rows in the DataFrame between 2022-01-02 and 2022-01-06.

The following example shows how to use this syntax in practice.

Example: Select Rows Between Two Dates in Pandas

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'date': pd.date_range(start='1/1/2022', periods=8),
                   'sales': [18, 20, 15, 14, 10, 9, 8, 12],
                   'returns': [5, 7, 7, 9, 12, 3, 2, 4]})

#view DataFrame
print(df)

        date  sales  returns
0 2022-01-01     18        5
1 2022-01-02     20        7
2 2022-01-03     15        7
3 2022-01-04     14        9
4 2022-01-05     10       12
5 2022-01-06      9        3
6 2022-01-07      8        2
7 2022-01-08     12        4

We can use the following syntax to select only the rows that fall between the date 2022-01-02 and 2022-01-06:

#select all rows where date is between 2022-01-02 and 2022-01-06
df[df.date.between('2022-01-02', '2022-01-06')]

              date      sales   returns
1	2022-01-02	20	7
2	2022-01-03	15	7
3	2022-01-04	14	9
4	2022-01-05	10	12
5	2022-01-06	9	3

Notice that only the rows between the dates 2022-01-02 and 2022-01-06 are selected.

If you’d like, you can also define the start and end dates outside of the between() function:

#define start and end dates
start_date = '2022-01-02'
end_date = '2022-01-06'

#select all rows where date is between start and end
df[df.date.between(start_date, end_date)]


              date	sales	returns
1	2022-01-02	20	7
2	2022-01-03	15	7
3	2022-01-04	14	9
4	2022-01-05	10	12
5	2022-01-06	9	3

This produces the same result.

Note that if your date column is not in a recognizable datetime format, you may first need to use the following code to convert it to a datetime format:

df['date'] = pd.to_datetime(df['date']) 

Once you’ve done this, you can proceed to use the between() function to select rows between specific dates.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Create a Date Range in Pandas
How to Extract Month from Date in Pandas
How to Convert Timestamp to Datetime in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories