Home > Mobile >  Looping through a range of Dates to appened dataframe
Looping through a range of Dates to appened dataframe

Time:02-02

I want to get the high at a specific interval of the day and loop it through the dates in the data frame, so I can then have a data frame with the High of the specified interval for every single day.

DataFrame I'm working with:

          Date         Time     Open     High      Low     Last
0      5/6/2019   09:30:00.0     2872   2888.5     2868  2888.25
1      5/6/2019   10:00:00.0  2888.25     2893   2883.5   2889.5
2      5/6/2019   10:30:00.0   2889.5  2895.25  2887.75  2894.25
3      5/6/2019   11:00:00.0     2894  2898.25  2891.25     2898
4      5/6/2019   11:30:00.0     2898   2898.5   2891.5  2892.25
5      5/6/2019   12:00:00.0  2892.75   2893.5  2890.25     2891
6      5/6/2019   12:30:00.0     2891   2894.5   2890.5   2890.5
7      5/6/2019   13:00:00.0  2890.25  2895.75  2888.75  2895.25
8      5/6/2019   13:30:00.0   2895.5   2896.5  2894.25  2896.25
9      5/6/2019   14:00:00.0  2896.25  2899.75  2894.75     2899
10     5/6/2019   14:30:00.0     2899  2909.75     2899   2909.5
11     5/6/2019   15:00:00.0  2909.25   2911.5  2906.75  2909.25
12     5/6/2019   15:30:00.0  2909.75     2912     2906  2907.75
13     5/7/2019   09:30:00.0  2879.75  2886.75   2869.5   2876.5
14     5/7/2019   10:00:00.0   2876.5   2876.5  2864.25  2874.75
15     5/7/2019   10:30:00.0   2874.5   2875.5  2861.25  2863.25
16     5/7/2019   11:00:00.0  2863.25  2868.25     2858     2865
17     5/7/2019   11:30:00.0  2865.25     2869   2856.5     2860
18     5/7/2019   12:00:00.0  2859.75  2868.75   2855.5     2868
19     5/7/2019   12:30:00.0     2868   2869.5   2862.5   2862.5
20     5/7/2019   13:00:00.0   2862.5   2863.5  2847.75  2849.75
21     5/7/2019   13:30:00.0  2849.75     2855  2845.25  2850.75
22     5/7/2019   14:00:00.0  2850.75     2855  2845.25     2846
23     5/7/2019   14:30:00.0     2846     2851     2841  2848.75
24     5/7/2019   15:00:00.0   2848.5  2852.25     2843     2845
25     5/7/2019   15:30:00.0  2844.75  2860.75   2836.5  2859.25

How would I be able to loop through the dates in the dataframe and get the 12:00:00.0 High for every single date in the dataframe

Desired Output (Dataframe):

          Date         Time     High      
0      5/6/2019   12:00:00.0  2893.5  
1      5/7/2019   12:00:00.0  2868.75

CodePudding user response:

You can filter right before a GroupBy.max :

m = df["Time"].eq("12:00:00.0")

out = df[m].groupby(["Date", "Time"], as_index=False)["High"].max()

Or simply, use loc (if you're sure to have unique pairs of Date/Time) :

out = df.loc[m, ["Date", "Time", "High"]]

​ Output :

print(out)

       Date        Time     High
0  5/6/2019  12:00:00.0  2893.50
1  5/7/2019  12:00:00.0  2868.75
  • Related