[英]Pandas nunique got an expected
我正在嘗試學習熊貓,並且nunique
了groupby
和nunique
一個小問題。
在下面創建數據框:
import pandas as pd
import numpy as np
chicago_dataset = pd.read_csv("https://raw.githubusercontent.com/gjreda/gregreda.com/master/content/notebooks/data/city-of-chicago-salaries.csv" ,
converters={'Employee Annual Salary': lambda x: float(x.replace('$', ''))})
帶有標題:
chicago_dataset.columns
Out[63]: Index(['Name', 'Position Title', 'Department', 'Employee Annual Salary'], dtype='object')
現在,我想按Department
分組並獲得不同的Position Title
,我該怎么做?
by_dept = chicago_dataset.groupby('Department')
我可以使用“ Name
如下所示,但“ Position Title
是兩個字。
by_dept.Name.nunique()
有兩種不同的方法可以做到這一點:
df.valid_python_name
df['any string with spaces and # etc.']
使用第二個:
by_dept['Position Title'].nunique()
例:
>>> by_dept['Position Title'].nunique().head()
Department
ADMIN HEARNG 15
ANIMAL CONTRL 19
AVIATION 125
BOARD OF ELECTION 23
BOARD OF ETHICS 9
Name: Position Title, dtype: int64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.