如何在类别而不是浮子上创建滚动含义?

发布于 2025-02-08 10:51:39 字数 511 浏览 3 评论 0原文

我的数据框中有一个类别的列。我知道如何在具有浮点值的列上进行滚动的含义:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2025-02-15 10:51:39

也许您可以使用pd.factorize

# Detection
s = pd.Series(pd.factorize(df['col1'])[0])
m = s.sub(s.rolling(10, center=True, min_periods=1).median()).abs().ge(1)
print(df[m])

# Output
  col1
9    S

Update

如果您正在寻找Mode,请尝试:

value, letter = pd.factorize(df['category'])
df['newcat'] = (pd.Series(value).rolling(10, center=True, min_periods=1)
                  .apply(lambda x: x.mode()).map(pd.Series(letter)))
print(df)

# Output
    id category newcat
0    1        A      A
1    2        A      A
2    3        A      A
3    4        A      A
4    5        A      A
5    6        A      A
6    7        S      A  # HERE
7    8        S      A  # HERE
8    9        A      A
9   10        A      A
10  11        A      A
11  12        A      A
12  13        A      A
13  14        A      A
14  15        A      A
15  17        A      A
16  18        A      A
17  19        A      A
18  20        A      A

Maybe you can use pd.factorize:

# Detection
s = pd.Series(pd.factorize(df['col1'])[0])
m = s.sub(s.rolling(10, center=True, min_periods=1).median()).abs().ge(1)
print(df[m])

# Output
  col1
9    S

Update

If you are looking for mode, try:

value, letter = pd.factorize(df['category'])
df['newcat'] = (pd.Series(value).rolling(10, center=True, min_periods=1)
                  .apply(lambda x: x.mode()).map(pd.Series(letter)))
print(df)

# Output
    id category newcat
0    1        A      A
1    2        A      A
2    3        A      A
3    4        A      A
4    5        A      A
5    6        A      A
6    7        S      A  # HERE
7    8        S      A  # HERE
8    9        A      A
9   10        A      A
10  11        A      A
11  12        A      A
12  13        A      A
13  14        A      A
14  15        A      A
15  17        A      A
16  18        A      A
17  19        A      A
18  20        A      A
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文