程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[pandas] count value_ counts( )

編輯:Python
Enjoy the beautiful picture 2022/06/21

value_counts() Method returns a Sequence Series, The sequence contains the number of each value ( For any column in the data frame ,value_counts() Method returns a count of each item in the column )

value_counts() yes Series The way to own , Generally in DataFrame When used in , You need to specify which column to use

grammar

value_counts(values,
sort=True,
ascending=False,
normalize=False,
bins=None,
dropna=True)

Parameter description

sort: Whether to sort ( Sort by default , The value is True)

ascending: Default descending sort ( The value is False), The value of ascending sort is True

normalize: Whether to standardize the calculation results , And display the results after standardization , The default is False

bins: You can customize the grouping interval , Default whether

dropna: Whether to include the right to NaN Count , The default does not include

import pandas as pd
import numpy as np
df = pd.DataFrame({'City': [' Beijing ', ' Guangzhou ', ' Shenzhen ', ' Shanghai ', ' dalian ', ' Chengdu ', ' Shenzhen ', ' Xiamen ', ' Beijing ', ' Beijing ', ' Shanghai ', ' zhuhai '],
'Revenue': [10000, 10000, 5000, 5000, 40000, 50000, 8000, 5000, 5000, 5000, 10000, 12000],
'Age': [50, 43, 34, 40, 25, 25, 45, 32, 25, 25, 34, np.nan]})
# 1. see 'City' The count result of this column ( Count and sort each value in a given column in descending order , Missing value nan Will also be excluded )
# value_counts() Not without any parameters , Instead, all parameters are default
res1 = df['City'].value_counts()
# 2. see 'Revenue' The count result of this column ( In ascending order )
res2 = df['Revenue'].value_counts(ascending=True)
# 3. see 'Age' The percentage of counts in this column ( Use standardization normalize=True)
res3 = df['Age'].value_counts(ascending=True,normalize=True)
# 4. see 'Age' The count result of this column ( Exhibition NaN Count of values )
res4 = df['Age'].value_counts(dropna=False)
# 5. see 'Age' The count result of this column ( Don't show NaN Count of values )
# res5 = df['Age'].value_counts()
res5 = df['Age'].value_counts(dropna=True)

df

res1

res2

res3 

res4 

res5 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved