Skip to content

Commit

Permalink
182
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhenguo committed Dec 6, 2020
1 parent cec2c5d commit c71f260
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
| 179 | [使用map对列做特征工程](./md/179.md) | pandas map | v1.0 | ⭐️⭐⭐ |
| 180 | [category列转数值](./md/180.md) | pandas category | v1.0 | ⭐️⭐⭐ |
| 181 | [rank排名](./md/181.md) | pandas rank | v1.0 | ⭐️⭐⭐ |
| 182 | [对数据下采样,调整小时步长为天](./md/182.md) | pandas resample | v1.0 | ⭐️⭐⭐ |

### Python 实战

Expand Down
Binary file added img/182-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/182-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 34 additions & 2 deletions md/182.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@

```markdown
@author jackzhenguo
@desc
@desc 完成数据下采样,步长小时调整为天
@tag
@version
@date 2020/03/20
```

182 如何完成数据下采样,调整步长由小时为天?

步长为小时的时间序列数据,有没有小技巧,快速完成下采样,采集成按天的数据呢?
先生成测试数据:
```python
import pandas as pd
import numpy as np
```

```python
df = pd.DataFrame(np.random.randint(1,10,size=(240,3)), \
columns = ['商品编码','商品销量','商品库存'])
```

```python
df.index = pd.util.testing.makeDateIndex(240,freq='H')
df
```

生成 240 行步长为小时间隔的数据:

![](../img/182-1.png)

小技巧,使用 resample 方法,合并为天(D)
```python
day_df = df.resample("D")["商品销量"].sum().to_frame()
day_df
```

结果如下,10行,240小时,正好为 10 days:

![](../img/182-2.png)

0 comments on commit c71f260

Please sign in to comment.