我是 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
提交
取消