[英]A better way to aggregate data and keep table structure and column names with Pandas
[英]Pandas Aggregate Data By Column Names and sum the data inside with step of 5
我有這樣的數據:
timestamp 101 100 105 109 110 112
2020-11-01 12:00:00 4 3 5 0 0 1
2020-11-01 12:01:00 4 9 5 3 1 1
2020-11-01 12:02:00 4 15 0 3 2 2
2020-11-01 12:03:00 4 15 0 3 2 2
2020-11-01 12:05:00 4 15 0 3 2 3
2020-11-01 12:06:00 4 15 0 3 2 0
我希望它以 5 為步長按列分組並對內部數據求和結果數據框應該在進行聚合之前首先對列進行排序:
timestamp 100 105 110
2020-11-01 12:00:00 12 0 1
2020-11-01 12:01:00 18 4 2
2020-11-01 12:02:00 19 5 4
2020-11-01 12:03:00 19 5 4
...
...
另外,想用前一行數據添加缺失的行 (12:04:00)
試試
out = df.set_index('timestamp').groupby(lambda x : int(x)//5*5,axis=1).sum()
Out[295]:
100 105 110
timestamp
2020-11-0112:00:00 7 5 1
2020-11-0112:01:00 13 8 2
2020-11-0112:02:00 19 3 4
2020-11-0112:02:00 19 3 4
2020-11-0112:02:00 19 3 5
2020-11-0112:02:00 19 3 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.