I am trying to learn to use filter to get rows based on the below conditions.
- Checking if col-a contains T2 and
- Checking if col-b has a time stamp between 7 and 9
I thought filter is a cool way to do this with few lines of code. But I haven't been able to get the desired output which is rows that satisfy the above conditions. What are other simple pythonic ways are there to do this (maybe where?). I'd appreciate any help in understanding how Filter works.
import pandas as pd
dict = {'col-a': ['abcd.T1.123', 'xyz.T2.456', 'xyz.T2.456'],
'col-b': ['07:57:00', '09:17:00', '12:57:00'],
}
# Filtering based on col-a - contains T-id
original_df = pd.DataFrame(dict)
print("\n ORIGINAL DF\n", original_df)
filtered_a_df = original_df.filter(like='.T2', axis=0)
print("\n FILTERED DF\n", filtered_a_df)
# Filtering based on col-b - time between 7 and 9
filtered_b_df = original_df.filter(regex='^0[79]:', axis=0)
print("\n FILTERED DF\n", filtered_b_df)