Home > Mobile >  How can i divide my database into 7 rows each time
How can i divide my database into 7 rows each time

Time:12-06

I have a data frame in which I have the Date and Days column and I want to divide my data frame into 7 days (week) and print one by one a week's date. enter image description here

0   2017-01-01  Sunday
1   2017-01-02  Monday
2   2017-01-03  Tuesday
3   2017-01-04  Wednesday
4   2017-01-05  Thursday
5   2017-01-06  Friday
6   2017-01-07  Saturday

then next time print next 7 days

7   2017-01-08  Sunday
8   2017-01-09  Monday
9   2017-01-10  Tuesday
10  2017-01-11  Wednesday
11  2017-01-12  Thursday
12  2017-01-13  Friday
13  2017-01-14  Saturday 

and so on.

Please help me to solve this problem.

CodePudding user response:

Compare values by Sunday with Series.cumsum for groups, for first group with 7 rows is necessary first row starting by Sunday:

for i, g in df.groupby(df['Day'].eq('Sunday').cumsum()):
    print (g)

If need list of DataFrames:

dfs = [g for for i, g in df.groupby(df['Day'].eq('Sunday').cumsum())]
  • Related