With the following list and dataframe I would like to iterate over the list of dates and create a new dataframe for each date that is in the list and dataframe.
list:
['12/12/2017', '12/13/2017', '12/14/2017']
dataframe:
source time date
0 a 8:00 12/12/2017
1 b 9:00 12/13/2017
2 c 8:30 12/12/2017
3 b 8:05 12/14/2017
4 a 8:10 12/13/2017
5 a 8:15 12/12/2017
Desired output:
source time date
0 a 8:00 12/12/2017
2 c 8:30 12/12/2017
5 a 8:15 12/12/2017
...and so on for each date.
code:
import pandas as pd
unique_dates=['12/12/2017','12/13/2017','12/14/2017']
data=[['a','8:00','12/12/2017'],['b','9:00','12/13/2017'],['c','8:30','12/12/2017'],
['b','8:05','12/14/2017'],['a','8:10','12/13/2017'],['a','8:15','12/12/2017']]
headers=['source','time','date']
df=pd.DataFrame(data,columns=headers)
for item in unique_dates:
if item in df:
new_df=df[df['date']==item]
print(new_df)
This gives me no output. I'm at a loss for how to do this and any help would be appreciated.