groupby()や.agg()はよく使いますので理解できるのですが、
groupby()
を使うなら、df.groupby(df['Date'] >= '2021-08-01')
となるでしょう。
python
1import pandas as pd
2import numpy as np
3
4N = 60
5df = pd.DataFrame({
6 "Date": pd.date_range(start='2021/7/1', periods=N, freq='D'),
7 "Player": [f'Player {i}' for i in np.random.randint(1, 6, N)],
8 "Speed": np.random.sample(N) * 100.0,
9 "Angle": np.random.sample(N) * 40.0 - 20.0,
10 "Efficiency": np.random.sample(N) * 100.0,
11})
12
13print(df.head(), '\n', df.tail())
14
15_ = '''
16 Date Player Speed Angle Efficiency
170 2021-07-01 Player 5 19.424221 10.398202 26.556917
181 2021-07-02 Player 4 19.584696 6.788205 69.426798
192 2021-07-03 Player 1 99.390299 16.283226 81.342796
203 2021-07-04 Player 5 41.540630 -4.641508 32.071403
214 2021-07-05 Player 1 44.272788 6.083881 76.182775
22
23 Date Player Speed Angle Efficiency
2455 2021-08-25 Player 3 33.155351 19.529361 99.201132
2556 2021-08-26 Player 3 21.561071 13.919872 19.584945
2657 2021-08-27 Player 3 21.030010 -15.227838 67.279885
2758 2021-08-28 Player 3 5.028648 -6.847735 70.794531
2859 2021-08-29 Player 2 35.603358 -11.656739 99.720992
29'''
30
31# split to two dataframes
32sep_date = '2021-08-01'
33first_half, second_half = [
34 pd.DataFrame(f) for _, f in df.groupby(df['Date'] >= sep_date)]
35
36
37print(first_half.head(), '\n', first_half.tail())
38
39_ = '''
40 Date Player Speed Angle Efficiency
410 2021-07-01 Player 5 19.424221 10.398202 26.556917
421 2021-07-02 Player 4 19.584696 6.788205 69.426798
432 2021-07-03 Player 1 99.390299 16.283226 81.342796
443 2021-07-04 Player 5 41.540630 -4.641508 32.071403
454 2021-07-05 Player 1 44.272788 6.083881 76.182775
46
47 Date Player Speed Angle Efficiency
4826 2021-07-27 Player 3 66.456286 1.561090 31.683482
4927 2021-07-28 Player 1 6.832737 14.446787 14.163490
5028 2021-07-29 Player 1 51.565821 -19.700863 79.727559
5129 2021-07-30 Player 2 85.230726 11.855018 35.503564
5230 2021-07-31 Player 3 98.261812 -10.599585 72.857861
53'''
54
55print(second_half.head(), '\n', second_half.tail())
56
57_ = '''
58 Date Player Speed Angle Efficiency
5931 2021-08-01 Player 3 58.559183 -7.192560 74.589404
6032 2021-08-02 Player 5 41.987566 -14.270427 69.006067
6133 2021-08-03 Player 3 42.610050 -15.104464 80.391799
6234 2021-08-04 Player 3 90.539195 8.055154 87.525425
6335 2021-08-05 Player 4 0.329127 18.792771 74.861874
64
65 Date Player Speed Angle Efficiency
6655 2021-08-25 Player 3 33.155351 19.529361 99.201132
6756 2021-08-26 Player 3 21.561071 13.919872 19.584945
6857 2021-08-27 Player 3 21.030010 -15.227838 67.279885
6958 2021-08-28 Player 3 5.028648 -6.847735 70.794531
7059 2021-08-29 Player 2 35.603358 -11.656739 99.720992
71'''