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.py514 chars16 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.
gistlibby LogSnag