Python Pandas subsetting based on Dates
Question: I got a dataframe (using pandas) which contains the following fields:

  1. Datesf-----------Price
  2. 02/08/16 17:28--10
  3. 02/08/16 17:29--20
  4. 02/08/16 17:30--30
  5. 03/08/16 09:00--40
  6. 04/08/16 09:00--50

I am trying to subset the data frame into new dataframes using "Datesf" as a
filter. The subsetting should only use the Datesf.Date() part of variable
"Datesf" and name the new dataframe "df" as df_date. for example> new
subsetted Dataframe name> df_02_08_16  
1\. Datesf------------Price  
2\. 02/08/16 17:28--10 3\. 02/08/16 17:29--20 4\. 02/08/16 17:30--30

I tried using the following code but obviously, I am missing out quite a few
bits:

    
    
    datelist= df["Datesf"].map(pd.Timestamp.date).unique()
    
    for d in datelist:
        print d
       df.loc[df['Datesf'] == '%s' % d]
    

My python skills are relatively basic at this stage. so forgive me if my query
is not so challenging. Many thanks. regards, S


Answer: This should do the work.

    
    
    import pandas as pd
    df = pd.DataFrame([['02/08/16 17:28',1], ['02/08/16 17:28',10],['02/08/16 17:28',100],['03/08/16 17:28',101],['04/08/16 17:28',103]], columns=['Datesf', 'Price'])
    df.Datesf = pd.to_datetime(df.Datesf)
    
    unique_dates = df.Datesf.unique()
    data_frame_dict = {elem : pd.DataFrame for elem in unique_dates}
    
    for n, key in enumerate(data_frame_dict.keys()):
        print ' ==== dataframe %d ======' % n
        data_frame_dict[key] = df[:][df.Datesf == key]
        print data_frame_dict[key]
        data_frame_dict[key].to_csv('%s.csv'%str(key))
    

