pandas中.value_counts()的用法


原文鏈接:https://www.jianshu.com/p/f773b4b82c66

value_counts()是一種查看表格某列中有多少個不同值的快捷方法,並計算每個不同值有在該列中有多少重復值。
value_counts()是Series擁有的方法,一般在DataFrame中使用時,需要指定對哪一列或行使用,該函數返回的也是Series類型,且index為該列的不同值,values為不同值的個數

1 import pandas as pd
2 import numpy as np
3 filepath='C:\python\data_src\GFSCOFOG_03-05-2018 03-04-36-54_timeSeries\GFSCOFOG_CHA.csv'
4 data = pd.read_csv(filepath,encoding='utf-8')

數據樣例如下圖所示

查看Unit Name中有哪些不同的值,並計算每個值有多少個重復值

data['Unit Name'].value_counts()

 

1 data['Unit Name'].value_counts()
2 #輸出
3 Percent of GDP                  3561
4 Domestic currency               3561
5 Percent of total expenditure     470
6 Name: Unit Name, dtype: int64

查看Sector Name中有哪些不同的值,並計算每個值有多少個重復值

data['Sector Name'].value_counts()

 1 data['Sector Name'].value_counts()
 2 #輸出結果
 3 Extrabudgetary central government                   1020
 4 Social security funds                               1002
 5 Central government (incl. social security funds)     944
 6 Budgetary central government                         944
 7 Local governments                                    944
 8 General government                                   944
 9 Central government (excl. social security funds)     944
10 State governments                                    850
11 Name: Sector Name, dtype: int64

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM