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

查看:39
本文介绍了如何根据值计数过滤 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).

帮助?

推荐答案

使用 groupby 过滤器:

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

我建议阅读文档的split-combine-section.

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

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