简体   繁体   中英

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:

  • The csv I am using for the example is this one .
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?

In the example, the filtered dataframe would have the information (EmployeeName, BasePay, Id...) about each employee that have an unique JobTitle.

IIUC transformnunique

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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