python - 将月的数量平均分配到周 (2)

标签 python pandas dataframe

想要将 df.Month 分解为 Weeks 并将数量平均分配给 Weeks。一周从星期一开始。

df

    Country         Item        Month           Qty
    -------------------------------------------
0   New Zealand     Apple       2017-10-31      100 
1   Puerto Rico     Banana      2017-11-30      200 
2   France          Apple       2017-10-31      400
...

所需的输出是:

    Country     Item        Week            Qty
    -------------------------------------------
0   New Zealand Apple       2017-10-01      20
1   New Zealand Apple       2017-10-08      20
2   New Zealand Apple       2017-10-15      20
3   New Zealand Apple       2017-10-22      20
4   New Zealand Apple       2017-10-29      20
5   Puerto Rico Banana      2017-11-05      50
6   Puerto Rico Banana      2017-11-12      50
7   Puerto Rico Banana      2017-11-19      50
8   Puerto Rico Banana      2017-11-26      50
9   France      Apple       2017-10-01      80
10  France      Apple       2017-10-08      80

...

使用以下方法制作了数周的数据框: mondays = pd.Series(pd.date_range(first_day, last_day, freq='W-Mon')) weeks = pd.DataFrame({'Week':mondays})

2)周

    Week
    ----------
0   2017-10-01 
1   2017-10-08 
2   2017-10-15 
3   2017-10-22 
4   2017-10-29 
5   2017-11-05 
6   2017-11-12 
7   2017-11-19 
8   2017-11-26 

...

这是问题的扩展:Distribute month's quantity equally into weeks .

最佳答案

您可以使用:

mondays = pd.Series(pd.date_range('2017-10-01', '2017-11-26 ', freq='W-Mon'))
weeks = pd.DataFrame({'Week':mondays})

#month period for merge
df['Month'] = pd.to_datetime(df['Month']).dt.to_period('m')
weeks['Week'] = pd.to_datetime(weeks['Week'])
#month period for merge
weeks['Month'] = weeks['Week'].dt.to_period('m')

#merge by Month
df = pd.merge(df, weeks, on='Month')
#divide by map by Series created by count
df['Qty'] = df['Qty'].div(df['Month'].map(weeks['Month'].value_counts()))
df = df.drop('Month', 1)
print (df)
        Country    Item        Qty       Week
0   New Zealand   Apple  20.000000 2017-10-02
1   New Zealand   Apple  20.000000 2017-10-09
2   New Zealand   Apple  20.000000 2017-10-16
3   New Zealand   Apple  20.000000 2017-10-23
4   New Zealand   Apple  20.000000 2017-10-30
5        France   Apple  80.000000 2017-10-02
6        France   Apple  80.000000 2017-10-09
7        France   Apple  80.000000 2017-10-16
8        France   Apple  80.000000 2017-10-23
9        France   Apple  80.000000 2017-10-30
10  Puerto Rico  Banana  66.666667 2017-11-06
11  Puerto Rico  Banana  66.666667 2017-11-13
12  Puerto Rico  Banana  66.666667 2017-11-20

关于python - 将月的数量平均分配到周 (2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46772401/

相关文章:

python - 在文本浏览器中使用 Django 的 https 站点抛出 CSRF 验证失败

python - pytest fixtures 的范围可以被覆盖吗?

Python - 导入文件 - SyntaxError : invalid syntax

python - 命令在没有索引的情况下打印前 10 行 python pandas 数据帧?

jquery - 将 ajax http post 转换为 python

rpy2 不会转换回 pandas

python - 如何根据第一列和第二列之间的差异获取数据框中第三列的值?

python - Pandas/Python 中的分块、处理和合并数据集

r - 基于列的分组创建数据帧子集的向量

python - 计算 DataFrame 中的行增量值