user14383280
user14383280

Reputation:

How make dataframes with loop row? Pandas

I am trying to get some new dataframes from one by column "day_of_week". Can I automate this process with loop row?

grouped = sleep.groupby('day_of_week')
    
monday    = grouped.get_group(0).loc[:, 'df']
tuesday   = grouped.get_group(1).loc[:, 'df']
wednesday = grouped.get_group(2).loc[:, 'df']

Sample of Data:

          Date  Sleep  Hours  Alimem  DoW
8  2020-01-09      4    9.6       0    3
9  2020-01-10      0    4.8       0    4
10 2020-01-11      0    0.0       0    5
11 2020-01-12      0    0.0       0    6
13 2020-01-13      0    7.2       0    0
14 2020-01-14      0    7.2       0    1
15 2020-01-15      0    7.2       0    2
16 2020-01-16      3    7.2       0    3
17 2020-01-17      0    6.4       0    4
18 2020-01-18      0    2.4       0    5
19 2020-01-19      0    2.4       0    6
20 2020-01-20      0    7.2       0    0
21 2020-01-21      0    7.2       0    1
22 2020-01-22      3    7.2       0    2
23 2020-01-23      0    7.2       0    3
24 2020-01-24      0    6.4       0    4
25 2020-01-25      0    3.2       0    5

Upvotes: 0

Views: 54

Answers (1)

techytushar
techytushar

Reputation: 803

This is one way to do it

days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
grouped = sleep.groupby('day_of_week')
groups = {days[i]: grouped.get_group(i).loc[:, 'df'] for i in range(len(days))}

Upvotes: 1

Related Questions