Python 數(shù)據(jù)的累加與統(tǒng)計(jì)的示例代碼
問題
你需要處理一個(gè)很大的數(shù)據(jù)集并需要計(jì)算數(shù)據(jù)總和或其他統(tǒng)計(jì)量。
解決方案
對(duì)于任何涉及到統(tǒng)計(jì)、時(shí)間序列以及其他相關(guān)技術(shù)的數(shù)據(jù)分析問題,都可以考慮使用 Pandas庫(kù) 。
為了讓你先體驗(yàn)下,下面是一個(gè)使用Pandas來分析芝加哥城市的 老鼠和嚙齒類動(dòng)物數(shù)據(jù)庫(kù) 的例子。 在我寫這篇文章的時(shí)候,這個(gè)數(shù)據(jù)庫(kù)是一個(gè)擁有大概74,000行數(shù)據(jù)的CSV文件。
>>> import pandas >>> # Read a CSV file, skipping last line >>> rats = pandas.read_csv('rats.csv', skip_footer=1) >>> rats <class 'pandas.core.frame.DataFrame'> Int64Index: 74055 entries, 0 to 74054 Data columns: Creation Date 74055 non-null values Status 74055 non-null values Completion Date 72154 non-null values Service Request Number 74055 non-null values Type of Service Request 74055 non-null values Number of Premises Baited 65804 non-null values Number of Premises with Garbage 65600 non-null values Number of Premises with Rats 65752 non-null values Current Activity 66041 non-null values Most Recent Action 66023 non-null values Street Address 74055 non-null values ZIP Code 73584 non-null values X Coordinate 74043 non-null values Y Coordinate 74043 non-null values Ward 74044 non-null values Police District 74044 non-null values Community Area 74044 non-null values Latitude 74043 non-null values Longitude 74043 non-null values Location 74043 non-null values dtypes: float64(11), object(9) >>> # Investigate range of values for a certain field >>> rats['Current Activity'].unique() array([nan, Dispatch Crew, Request Sanitation Inspector], dtype=object) >>> # Filter the data >>> crew_dispatched = rats[rats['Current Activity'] == 'Dispatch Crew'] >>> len(crew_dispatched) 65676 >>> >>> # Find 10 most rat-infested ZIP codes in Chicago >>> crew_dispatched['ZIP Code'].value_counts()[:10] 60647 3837 60618 3530 60614 3284 60629 3251 60636 2801 60657 2465 60641 2238 60609 2206 60651 2152 60632 2071 >>> >>> # Group by completion date >>> dates = crew_dispatched.groupby('Completion Date') <pandas.core.groupby.DataFrameGroupBy object at 0x10d0a2a10> >>> len(dates) 472 >>> >>> # Determine counts on each day >>> date_counts = dates.size() >>> date_counts[0:10] Completion Date 01/03/2011 4 01/03/2012 125 01/04/2011 54 01/04/2012 38 01/05/2011 78 01/05/2012 100 01/06/2011 100 01/06/2012 58 01/07/2011 1 01/09/2012 12 >>> >>> # Sort the counts >>> date_counts.sort() >>> date_counts[-10:] Completion Date 10/12/2012 313 10/21/2011 314 09/20/2011 316 10/26/2011 319 02/22/2011 325 10/26/2012 333 03/17/2011 336 10/13/2011 378 10/14/2011 391 10/07/2011 457 >>>
嗯,看樣子2011年10月7日對(duì)老鼠們來說是個(gè)很忙碌的日子啊!^_^
討論
Pandas是一個(gè)擁有很多特性的大型函數(shù)庫(kù),我在這里不可能介紹完。 但是只要你需要去分析大型數(shù)據(jù)集合、對(duì)數(shù)據(jù)分組、計(jì)算各種統(tǒng)計(jì)量或其他類似任務(wù)的話,這個(gè)函數(shù)庫(kù)真的值得你去看一看。
以上就是Python 數(shù)據(jù)的累加與統(tǒng)計(jì)的方法的詳細(xì)內(nèi)容,更多關(guān)于Python 數(shù)據(jù)的累加與統(tǒng)計(jì)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 用python按照?qǐng)D像灰度值統(tǒng)計(jì)并篩選圖片的操作(PIL,shutil,os)
- python統(tǒng)計(jì)字符串中字母出現(xiàn)次數(shù)代碼實(shí)例
- python統(tǒng)計(jì)文章中單詞出現(xiàn)次數(shù)實(shí)例
- Python統(tǒng)計(jì)文本詞匯出現(xiàn)次數(shù)的實(shí)例代碼
- python統(tǒng)計(jì)函數(shù)庫(kù)scipy.stats的用法解析
- python實(shí)現(xiàn)數(shù)據(jù)分析與建模
- python 應(yīng)用之Pycharm 新建模板默認(rèn)添加編碼格式-作者-時(shí)間等信息【推薦】
- Python基于Logistic回歸建模計(jì)算某銀行在降低貸款拖欠率的數(shù)據(jù)示例
- Python內(nèi)建模塊struct實(shí)例詳解
- 詳解在Python的Django框架中創(chuàng)建模板庫(kù)的方法
- Python創(chuàng)建模塊及模塊導(dǎo)入的方法
- Python進(jìn)行統(tǒng)計(jì)建模
相關(guān)文章
關(guān)于numpy和torch.tensor的張量的操作
這篇文章主要介紹了關(guān)于numpy和torch.tensor的張量的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Python?數(shù)據(jù)可視化神器Pyecharts繪制圖像練習(xí)
這篇文章主要介紹了Python?數(shù)據(jù)可視化神器Pyecharts繪制圖像練習(xí),繪制的圖形有柱狀圖、餅狀圖、箱型圖、折線圖、雷達(dá)圖等多種圖像,需要的小伙伴可以參考一下2022-02-02Pandas多個(gè)條件(AND,OR,NOT)中提取行
本文主要介紹了Pandas多個(gè)條件(AND,OR,NOT)中提取行,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python企業(yè)編碼生成系統(tǒng)總體系統(tǒng)設(shè)計(jì)概述
這篇文章主要介紹了Python企業(yè)編碼生成系統(tǒng)總體系統(tǒng)設(shè)計(jì),簡(jiǎn)單描述了Python企業(yè)編碼生成系統(tǒng)的功能、結(jié)構(gòu)與相關(guān)編碼實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07Python實(shí)現(xiàn)基于權(quán)重的隨機(jī)數(shù)2種方法
這篇文章主要介紹了Python實(shí)現(xiàn)基于權(quán)重的隨機(jī)數(shù)2種方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04使用BeeWare實(shí)現(xiàn)iOS調(diào)用Python方式
這篇文章主要介紹了使用BeeWare實(shí)現(xiàn)iOS調(diào)用Python方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Python中使用字典對(duì)列表中的元素進(jìn)行計(jì)數(shù)的幾種方式
本文主要介紹了Python中使用字典對(duì)列表中的元素進(jìn)行計(jì)數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06