我是 python 和一般编程的新手。我希望下面的问题得到很好的解释。我有一个很大的数据集,有 80 多列,其中一些列只有每周的数据。我想将这些列转换为每天的值,只需将每周值除以 7 并将结果归因于值本身和该周的其他 6 天。这是我的输入数据集的样子: date col1 col2 col302-09-2019 14 NaN 109-09-2019 NaN NaN 216-09-2019 NaN 7 323-09-2019 NaN NaN 430-09-2019 NaN NaN 507-10-2019 NaN NaN 614-10-2019 NaN NaN 721-10-2019 21 NaN 828-10-2019 NaN NaN 904-11-2019 NaN 14 1011-11-2019 NaN NaN 11..输出应如下所示: date col1 col2 col302-09-2019 2 NaN 109-09-2019 2 NaN 216-09-2019 2 1 323-09-2019 2 1 430-09-2019 2 1 507-10-2019 2 1 614-10-2019 2 1 721-10-2019 3 1 828-10-2019 3 1 904-11-2019 3 2 10 11-11-2019 3 2 11..我无法想出解决方案,但我认为这可能有效:def convert_to_daily(df): for column in df.columns.tolist(): if column.isna(): # if true for line in range(len(df[column])): # check if value is not empty and succeeded by an 6 empty values or some better logic # I don´t know how to do that.
1 回答
侃侃尔雅
TA贡献1801条经验 获得超16个赞
我相信您需要选择包含至少一个缺失值的列,向前填充缺失值并除以7:
m = df.isna().any()
df.loc[:, m] = df.loc[:, m].ffill(limit=7).div(7)
print (df)
date col1 col2 col3
0 02-09-2019 2.0 NaN 1
1 09-09-2019 2.0 NaN 2
2 16-09-2019 2.0 1.0 3
3 23-09-2019 2.0 1.0 4
4 30-09-2019 2.0 1.0 5
5 07-10-2019 2.0 1.0 6
6 14-10-2019 2.0 1.0 7
7 21-10-2019 3.0 1.0 8
8 28-10-2019 3.0 1.0 9
9 04-11-2019 3.0 2.0 10
10 11-11-2019 3.0 2.0 11
添加回答
举报
0/150
提交
取消