如何在类别而不是浮子上创建滚动含义?
我的数据框中有一个类别的列。我知道如何在具有浮点值的列上进行滚动的含义:
df['rolling_mean'] = df.categorycolumn.rolling(10).mean()
但是我在本列中没有数字,而是字母:
A
A
A
A
D
D
D
D
D
S
D
D
D
D
D
我想摆脱s,所以我想尝试使平均滚动的平均值因此,它对异常的敏感性较小。有人知道该怎么做吗?
问候并享受阳光(如果有的话)!
PS实际上我不是在寻找均值,而是在某个窗口上最常见的类别价值。
因此我的数据框架看起来像这样:
id category
1 A
2 A
3 A
4 A
5 A
6 A
7 S
8 S
9 A
10 A
11 A
12 A
13 A
14 A
15 A
17 A
18 A
19 A
20 A
我不会找到像10这样的滚动窗口的模式,因此完整列只会变成(并且没有s)
I have a column in my data frame with a category. I know how to do a rolling mean on a column with float values:
df['rolling_mean'] = df.categorycolumn.rolling(10).mean()
But I do not have numbers in this column but letters, for example:
A
A
A
A
D
D
D
D
D
S
D
D
D
D
D
And I want to get rid of the S, so I want to try to make a rolling average so it will be less sensitive for anomalies. Does anyone know how to do this?
Greetings and enjoy the sun (if there is any)!
p.s. Actually I am not looking for the mean but for the mode, the most common category value over a certain window.
So my data frame looks like this:
id category
1 A
2 A
3 A
4 A
5 A
6 A
7 S
8 S
9 A
10 A
11 A
12 A
13 A
14 A
15 A
17 A
18 A
19 A
20 A
And I wont it to find the mode of a rolling window like 10, so the full column would become only A (and no S)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以使用
pd.factorize
:Update
如果您正在寻找
Mode
,请尝试:Maybe you can use
pd.factorize
:Update
If you are looking for
mode
, try: