熊猫.系列:如何获得下一个值的比率



关于熊猫,我想知道如何获得下一个值的比率。以下系列是一个示例。

import pandas as pd
s = pd.Series([1,2,1,1,1,3])
>>> s
0    1
1    2
2    1
3    1
4    1
5    3
# What I wanna get are below rates.
# 1 to 2 : 1/5(0.2)
# 2 to 1 : 1/5(0.2)
# 1 to 1 : 2/5(0.4)
# 1 to 3 : 1/5(0.2)

很抱歉描述不好,但有人知道怎么做吗?

一种可能的解决方案,包括按GroupBy.size聚合计数和按DataFrame长度除法:

import pandas as pd
import numpy as np
s = pd.Series([1,2,1,1,1,3])
def rolling_window(a, window):
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

df1 = pd.DataFrame(rolling_window(s.values, 2), columns=['from','to'])
df1 = df1.groupby(['from','to'], sort=False).size().div(len(df1)).reset_index(name='rate')
print (df1)
from  to  rate
0     1   2   0.2
1     2   1   0.2
2     1   1   0.4
3     1   3   0.2

最新更新