Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate over days (pandas)

Tags:

python

pandas

How can I iterate over days in the dataframe in pandas?

Example:

My dataframe:

                                   time  consumption
time
2016-10-17 09:00:00 2016-10-17 09:00:00  2754.483333
2016-10-17 10:00:00 2016-10-17 10:00:00  2135.966666
2016-10-17 11:00:00 2016-10-17 11:00:00  1497.716666
2016-10-17 12:00:00 2016-10-17 12:00:00   448.100000
2016-10-24 09:00:00 2016-10-24 09:00:00  1527.716666
2016-10-24 10:00:00 2016-10-24 10:00:00  1219.833333
2016-10-24 11:00:00 2016-10-24 11:00:00  1284.350000
2016-10-24 12:00:00 2016-10-24 12:00:00  14195.633333
2016-10-31 09:00:00 2016-10-31 09:00:00  2120.933333
2016-10-31 10:00:00 2016-10-31 10:00:00  1630.700000
2016-10-31 11:00:00 2016-10-31 11:00:00  1241.866666
2016-10-31 12:00:00 2016-10-31 12:00:00  1156.266666

Pseudocode:

for day in df:
    print day

First iteration return:

                                   time  consumption
time
2016-10-17 09:00:00 2016-10-17 09:00:00  2754.483333
2016-10-17 10:00:00 2016-10-17 10:00:00  2135.966666
2016-10-17 11:00:00 2016-10-17 11:00:00  1497.716666
2016-10-17 12:00:00 2016-10-17 12:00:00   448.100000

Second iteration return:

2016-10-24 09:00:00 2016-10-24 09:00:00  1527.716666
2016-10-24 10:00:00 2016-10-24 10:00:00  1219.833333
2016-10-24 11:00:00 2016-10-24 11:00:00  1284.350000
2016-10-24 12:00:00 2016-10-24 12:00:00  14195.633333

Third iteration return :

2016-10-31 09:00:00 2016-10-31 09:00:00  2120.933333
2016-10-31 10:00:00 2016-10-31 10:00:00  1630.700000
2016-10-31 11:00:00 2016-10-31 11:00:00  1241.866666
2016-10-31 12:00:00 2016-10-31 12:00:00  1156.266666
like image 812
volt Avatar asked Jan 27 '17 12:01

volt


1 Answers

Use groupby by date, what is a bit different as day:

#groupby by index date
for idx, day in df.groupby(df.index.date):
    print (day)
                                   time  consumption
time                                                
2016-10-17 09:00:00 2016-10-17 09:00:00  2754.483333
2016-10-17 10:00:00 2016-10-17 10:00:00  2135.966666
2016-10-17 11:00:00 2016-10-17 11:00:00  1497.716666
2016-10-17 12:00:00 2016-10-17 12:00:00   448.100000
                                   time   consumption
time                                                 
2016-10-24 09:00:00 2016-10-24 09:00:00   1527.716666
2016-10-24 10:00:00 2016-10-24 10:00:00   1219.833333
2016-10-24 11:00:00 2016-10-24 11:00:00   1284.350000
2016-10-24 12:00:00 2016-10-24 12:00:00  14195.633333
                                   time  consumption
time                                                
2016-10-31 09:00:00 2016-10-31 09:00:00  2120.933333
2016-10-31 10:00:00 2016-10-31 10:00:00  1630.700000
2016-10-31 11:00:00 2016-10-31 11:00:00  1241.866666
2016-10-31 12:00:00 2016-10-31 12:00:00  1156.266666

Or:

#groupby by column time
for idx, day in df.groupby(df.time.dt.date):
    print (day)
                                   time  consumption
time                                                
2016-10-17 09:00:00 2016-10-17 09:00:00  2754.483333
2016-10-17 10:00:00 2016-10-17 10:00:00  2135.966666
2016-10-17 11:00:00 2016-10-17 11:00:00  1497.716666
2016-10-17 12:00:00 2016-10-17 12:00:00   448.100000
                                   time   consumption
time                                                 
2016-10-24 09:00:00 2016-10-24 09:00:00   1527.716666
2016-10-24 10:00:00 2016-10-24 10:00:00   1219.833333
2016-10-24 11:00:00 2016-10-24 11:00:00   1284.350000
2016-10-24 12:00:00 2016-10-24 12:00:00  14195.633333
                                   time  consumption
time                                                
2016-10-31 09:00:00 2016-10-31 09:00:00  2120.933333
2016-10-31 10:00:00 2016-10-31 10:00:00  1630.700000
2016-10-31 11:00:00 2016-10-31 11:00:00  1241.866666
2016-10-31 12:00:00 2016-10-31 12:00:00  1156.266666

Differences can check in first 2 rows are changed with different month:

for idx, day in df.groupby(df.index.day):
    print (day)
                                    time  consumption
time                                                
2016-09-17 09:00:00 2016-10-17 09:00:00  2754.483333
2016-09-17 10:00:00 2016-10-17 10:00:00  2135.966666
2016-10-17 11:00:00 2016-10-17 11:00:00  1497.716666
2016-10-17 12:00:00 2016-10-17 12:00:00   448.100000
                                   time   consumption
time                                                 
2016-10-24 09:00:00 2016-10-24 09:00:00   1527.716666
2016-10-24 10:00:00 2016-10-24 10:00:00   1219.833333
2016-10-24 11:00:00 2016-10-24 11:00:00   1284.350000
2016-10-24 12:00:00 2016-10-24 12:00:00  14195.633333
                                   time  consumption
time                                                
2016-10-31 09:00:00 2016-10-31 09:00:00  2120.933333
2016-10-31 10:00:00 2016-10-31 10:00:00  1630.700000
2016-10-31 11:00:00 2016-10-31 11:00:00  1241.866666
2016-10-31 12:00:00 2016-10-31 12:00:00  1156.266666
for idx, day in df.groupby(df.index.date):
    print (day)
                                   time  consumption
time                                                
2016-09-17 09:00:00 2016-10-17 09:00:00  2754.483333
2016-09-17 10:00:00 2016-10-17 10:00:00  2135.966666
                                   time  consumption
time                                                
2016-10-17 11:00:00 2016-10-17 11:00:00  1497.716666
2016-10-17 12:00:00 2016-10-17 12:00:00   448.100000
                                   time   consumption
time                                                 
2016-10-24 09:00:00 2016-10-24 09:00:00   1527.716666
2016-10-24 10:00:00 2016-10-24 10:00:00   1219.833333
2016-10-24 11:00:00 2016-10-24 11:00:00   1284.350000
2016-10-24 12:00:00 2016-10-24 12:00:00  14195.633333
                                   time  consumption
time                                                
2016-10-31 09:00:00 2016-10-31 09:00:00  2120.933333
2016-10-31 10:00:00 2016-10-31 10:00:00  1630.700000
2016-10-31 11:00:00 2016-10-31 11:00:00  1241.866666
2016-10-31 12:00:00 2016-10-31 12:00:00  1156.266666
like image 141
jezrael Avatar answered Sep 30 '22 00:09

jezrael