【问题标题】:Expanding a data set from months to days in Python for non-DateTime type data在 Python 中将非 DateTime 类型数据的数据集从几个月扩展到几天
【发布时间】:2020-05-07 00:21:22
【问题描述】:

如何将此月度表(表 A)扩展为将收入分布在 30 天期间的日表(表 B)?

表 A

index   Month   Revenue ($)
0          1    300
1          2    330
2          3    390

(假设每个月有30天)

表 B

index   Month   Day Revenue ($)
0         1      1   10
1         1      2   10
2         1      3   10
...      ...    ... ...
30        2      1   11
31        2      2   11
...      ...    ... ...
60        3      1   13
...      ...    ... ...
89        3     30   13

【问题讨论】:

    标签: python pandas data-manipulation expand days


    【解决方案1】:

    试试:

    df = pd.concat([df]*30).assign(Revenue=lambda x: x['Revenue'] / 30).sort_values('Month')

    创建天列

    df['day'] = [i for i in range(1, 31)] * number_of_months

    print(df)

        Month  Revenue  day
    0       1     10.0    1
    1       1     10.0    2
    2       1     10.0    3
    3       1     10.0    4
    4       1     10.0    5
    ..    ...      ...  ...
    85      3     13.0   26
    86      3     13.0   27
    87      3     13.0   28
    88      3     13.0   29
    89      3     13.0   30
    

    【讨论】:

    • 很高兴为您提供帮助 +1 随时提出您的任何问题
    • 能否请您告诉我您是如何编辑问题中发布的数据的?
    • 对代码块使用三重`,所以当显示一个数据框时用```括起来,看看编辑语法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 2015-08-02
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    相关资源
    最近更新 更多