本文介绍了Python pandas:排除低于特定频率计数的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个如下所示的 Pandas DataFrame:

So I have a pandas DataFrame that looks like this:

r vals    positions
1.2       1
1.8       2
2.3       1
1.8       1
2.1       3
2.0       3
1.9       1
...       ...

我希望按位置过滤掉所有不出现至少 20 次的行.我见过这样的

I would like the filter out all rows by position that do not appear at least 20 times. I have seen something like this

g=df.groupby('positions')
g.filter(lambda x: len(x) > 20)

但这似乎不起作用,我不明白如何从中获取原始数据帧.预先感谢您的帮助.

but this does not seem to work and I do not understand how to get the original dataframe back from this. Thanks in advance for the help.

推荐答案

在您有限的数据集上,以下工作:

On your limited dataset the following works:

In [125]:
df.groupby('positions')['r vals'].filter(lambda x: len(x) >= 3)

Out[125]:
0    1.2
2    2.3
3    1.8
6    1.9
Name: r vals, dtype: float64

您可以分配此过滤器的结果并将其与 isin 来过滤你的原始文件:

You can assign the result of this filter and use this with isin to filter your orig df:

In [129]:
filtered = df.groupby('positions')['r vals'].filter(lambda x: len(x) >= 3)
df[df['r vals'].isin(filtered)]

Out[129]:
   r vals  positions
0     1.2          1
1     1.8          2
2     2.3          1
3     1.8          1
6     1.9          1

您只需要在您的情况下将 3 更改为 20

You just need to change 3 to 20 in your case

另一种方法是使用 value_counts 来创建聚合系列,然后我们可以使用它来过滤您的 df:

Another approach would be to use value_counts to create an aggregate series, we can then use this to filter your df:

In [136]:
counts = df['positions'].value_counts()
counts

Out[136]:
1    4
3    2
2    1
dtype: int64

In [137]:
counts[counts > 3]

Out[137]:
1    4
dtype: int64

In [135]:
df[df['positions'].isin(counts[counts > 3].index)]

Out[135]:
   r vals  positions
0     1.2          1
2     2.3          1
3     1.8          1
6     1.9          1

编辑

如果你想过滤数据框上的 groupby 对象而不是一个系列,那么你可以调用 filter 直接在 groupby 对象上:

If you want to filter the groupby object on the dataframe rather than a Series then you can call filter on the groupby object directly:

In [139]:
filtered = df.groupby('positions').filter(lambda x: len(x) >= 3)
filtered

Out[139]:
   r vals  positions
0     1.2          1
2     2.3          1
3     1.8          1
6     1.9          1

这篇关于Python pandas:排除低于特定频率计数的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:44
查看更多