簡體   English   中英

Pandas nunique得到了期望

[英]Pandas nunique got an expected

我正在嘗試學習熊貓,並且nuniquegroupbynunique一個小問題。

在下面創建數據框:

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()

有兩種不同的方法可以做到這一點:

  1. df.valid_python_name
  2. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM