how to subset pandas datetime with str.contians in python

To subset a Pandas DataFrame by a column that contains a specific string and filter records by date and time, you can use the str.contains() method in combination with boolean indexing and the loc indexer. Here's an example:

main.py
import pandas as pd

# create a sample dataframe
df = pd.DataFrame({
    'DateTime': ['2022-06-01 10:30:00', '2022-06-01 12:45:00', '2022-06-02 09:15:00', '2022-06-02 16:30:00'],
    'Event': ['Meeting', 'Webinar', 'Training', 'Conference']
})

# convert DateTime column to datetime format
df['DateTime'] = pd.to_datetime(df['DateTime'])

# subset records with 'June 1' in DateTime column and convert to string format
filtered_df = df.loc[df['DateTime'].astype(str).str.contains('2022-06-01')]

print(filtered_df)
514 chars
16 lines

In this example, we first create a sample Pandas DataFrame with a DateTime column and an Event column. We then convert the DateTime column to datetime format using pd.to_datetime().

Next, we filter the DataFrame using boolean indexing and the str.contains() method. We first convert the DateTime column to a string using astype(str) and then apply str.contains('2022-06-01') to filter records that contain '2022-06-01' in the DateTime column.

Finally, we print the filtered DataFrame, which contains all records with 'June 1' in the DateTime column.

Note: You can replace '2022-06-01' string in str.contains() with any other date format based on your dataframe.

related categories

gistlibby LogSnag