Bre*_*ris 2 python pandas rolling-computation
I have a pandas dataframe containing string values and a datetime index, like so:
from datetime import datetime as dt
import pandas as pd
df = pd.DataFrame(['a', 'b', 'b', 'c', 'b', 'b', 'b'],
[dt(2019, 1, 1), dt(2019, 1, 2),
dt(2019, 1, 3), dt(2019, 1, 4),
dt(2019, 1, 5), dt(2019, 1, 6),
dt(2019, 1, 7)])
Run Code Online (Sandbox Code Playgroud)
If I wanted to compute the number of instances that each value occurs over all times, I can simply call:
>>> print(df[0].value_counts())
b 5
c 1
a 1
Name: 0, dtype: int64
Run Code Online (Sandbox Code Playgroud)
I'd like to create a rolling window and measure the number of instances of each string on a moving window of say, 2 days. Is there a way to combine rolling with value_counts, or similar?
我想您正在寻找的是:
pd.get_dummies(df[0]).rolling('2D').sum()
Run Code Online (Sandbox Code Playgroud)
输出:
a b c
2019-01-01 1.0 0.0 0.0
2019-01-02 1.0 1.0 0.0
2019-01-03 0.0 2.0 0.0
2019-01-04 0.0 1.0 1.0
2019-01-05 0.0 1.0 1.0
2019-01-06 0.0 2.0 0.0
2019-01-07 0.0 2.0 0.0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
185 次 |
| 最近记录: |