简体   繁体   English

在熊猫 value_counts() 中使用条件

[英]Using conditions in pandas value_counts()

When applying conditions based on value_counts() to an dataframe, a "boolean dataframe" is obtained, as in the example below:将基于 value_counts() 的条件应用于数据帧时,将获得“布尔数据帧”,如下例所示:

  • The csv I am using for the example is this one .我用于示例的 csv 就是这个
import pandas as pd
sal = pd.read_csv("Salaries.csv")
sal[sal["Year"] == 2013]["JobTitle"].value_counts() == 1

Instead of obtaining these booleans, is it possible to filter the dataframe in order to display the actual data of the rows that returned True to the condition?除了获取这些布尔值之外,是否可以过滤数据框以显示返回 True 条件的行的实际数据?

In the example, the filtered dataframe would have the information (EmployeeName, BasePay, Id...) about each employee that have an unique JobTitle.在该示例中,过滤后的数据框将包含关于每个具有唯一 JobTitle 的员工的信息(EmployeeName、BasePay、Id...)。

IIUC transformnunique

targetdf=sal[sal[sal["Year"] == 2013].groupby(["JobTitle"]).transform('nunique')==1].copy()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM