将非 DateTime 类型数据的数据集从月数扩展到天数 Python
Expanding a data set from months to days in Python for non-DateTime type data
我如何将每月 table(Table A) 扩展为每天 table(Table B) 以在 30 天内分配收入?
Table一个
index Month Revenue ($)
0 1 300
1 2 330
2 3 390
(假设每个月有 30 天)
Table 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
尝试:
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
我如何将每月 table(Table A) 扩展为每天 table(Table B) 以在 30 天内分配收入?
Table一个
index Month Revenue ($)
0 1 300
1 2 330
2 3 390
(假设每个月有 30 天)
Table 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
尝试:
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