简体   繁体   中英

How to group a date columns?

I have a csv file containing Date , X and Y columns.

    Date (UTC)        X         Y
0   2020-01-05    26.474679 -0.730455
1   2020-01-05    30.746291  0.020503
2   2020-01-05    37.829401  0.252316
3   2020-01-05  1904.611372  0.977388
4   2020-01-19    38.441813 -0.044736
5   2020-01-19    31.067455  0.419826
6   2020-01-19    15.972198 -0.592661
7   2020-01-19  1261.038155  0.977394
8   2020-02-02    36.628855  0.518991
9   2020-02-02    29.724500 -0.818267
10  2020-02-02   673.138440  0.977481
11  2020-02-02    13.119385  0.327124

I want the results look like this. I want to group all same dates 2020-01-05 , 2020-01-19 and 2020-02-02 .

How can I do it?

     Date (UTC)        X         Y   
 0  2020-01-05    26.474679 -0.730455   
 1  2020-01-05    30.746291  0.020503   
 2  2020-01-05    37.829401  0.252316   
 3  2020-01-05  1904.611372  0.977388 

     Date (UTC)        X         Y
 4  2020-01-19    38.441813 -0.044736   
 5  2020-01-19    31.067455  0.419826   
 6  2020-01-19    15.972198 -0.592661   
 7  2020-01-19  1261.038155  0.977394   

To generate separate CSV files for each date, you can run:

for key, grp in df.groupby('Date (UTC)'):
    fn = f'out_{key}.csv'
    print(f'Group {key}: {fn}')
    grp.to_csv(fn)

Assuming that your DataFrame contains rows with only the indicated dates, just 3 output files will be generated, with name including the date from which rows were taken.

Eg out_2020-01-05.csv file will contain:

,Date (UTC),X,Y
0,2020-01-05,26.474679,-0.730455
1,2020-01-05,30.746290999999996,0.020503
2,2020-01-05,37.829401000000004,0.252316
3,2020-01-05,1904.6113719999998,0.9773879999999999

But I doubt whether you acutally need separate DataFrames for each date. I think that it will be sufficient to generate a subset of rows for particular date, somenthing like df[df['Date (UTC)'] == '2020-01-19'] .

Note also that in the above loop grp contains just this subset (for date given by key ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM