python - 如何根据值计数过滤 Pandas DataFrame?

标签 python pandas filtering dataframe

我正在使用 Python 处理视频游戏的 pandas DataFrame,每个游戏都有一个类型。我正在尝试删除在 DataFrame 中出现次数少于某些类型的任何视频游戏,但我不知道如何去做。我确实找到了 a StackOverflow question这似乎是相关的,但我根本无法破译解决方案(可能是因为我从未听说过 R 并且我对函数式编程的内存充其量是生疏的)。

帮助?

最佳答案

使用 groupby filter :

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 of the docs .

关于python - 如何根据值计数过滤 Pandas DataFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29836836/

相关文章:

python - 使用 Python 从 Google Cloud Storage 下载大文件

python - 如何编写一个高效的__dict__重载函数?

python - 如何将包含可迭代项作为项目的字典转换为数据框

java - 使用对象的字段过滤 ArrayList

api - 是否有任何 REST API 查询标准/DSL 来表达 GET URL 中的复杂过滤器?

python - process_template_response 在 Django 中不会被调用

python - 为什么打开终端时会调用 conda?

python - 在空白行上拆分 Pandas DataFrame

powerbi - 需要在 Power BI Slicer 中将当前日期限制为上个月月底

python - 将带有 json 的 numpy 数组发布到带有请求的 flask 应用程序