如何根据值计数过滤 pandas DataFrame? [英] How do I filter a pandas DataFrame based on value counts?

查看:277
本文介绍了如何根据值计数过滤 pandas DataFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python处理电子游戏的Pandas DataFrame,每个都有一种类型.我正在尝试删除流派少于DataFrame中某些流派的任何视频游戏,但是我不知道如何进行此操作.我确实发现了一个StackOverflow问题,似乎是相关,但是我根本无法破译该解决方案(可能是因为我从未听说过R,并且我对函数式编程的记忆充其量是生锈的).

I'm working in Python with a pandas DataFrame of video games, each with a genre. I'm trying to remove any video game with a genre that appears less than some number of times in the DataFrame, but I have no clue how to go about this. I did find a StackOverflow question that seems to be related, but I can't decipher the solution at all (possibly because I've never heard of R and my memory of functional programming is rusty at best).

帮助?

推荐答案

使用分组过滤器:

In [11]: df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=['A', 'B'])

In [12]: df
Out[12]:
   A  B
0  1  2
1  1  4
2  5  6

In [13]: df.groupby("A").filter(lambda x: len(x) > 1)
Out[13]:
   A  B
0  1  2
1  1  4

我建议阅读文档的拆分组合部分.

这篇关于如何根据值计数过滤 pandas DataFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆