Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

python - groupby week - pandas dataframe

I would like to group a pandas dataframe by week from the last entry day, and make the sum for each column /per week.

(1 week : monday -> sunday, if the last entry is tuesday, the week is is composed of monday and tuesday data only, not today - 7 days)

df:

             a  b  c  d  e
2019-01-01   1  2  5  0  1
...
2020-01-25   2  3  6  1  0
2020-01-26   1  2  3  4  5

expected output:

       week  a  b  c  d  e 
       104   9  8  8  8  7
       ...
       1     7  8  8  8  9

code:

df = df.rename_axis('date').reset_index()
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
df.groupby(DF.date.dt.strftime('%W')).sum()

Problem: not the week number I want and the weeks n of each year are grouped in the same line

question from:https://stackoverflow.com/questions/65907974/groupby-week-pandas-dataframe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try extract the iso calendar (year-week-day), then groupby:

s = dt.index.isocalendar()

dt.groupby([s.year, s.week]).sum()

You would get something like this:

            a   b   c   d   e
year week                    
2019 1     18  33  31  26  25
     2     36  31  25  28  31
     3     33  22  44  22  29
     4     36  36  35  33  31
     5     27  30  26  31  36

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...