做者:xiaoyu微信公衆號:Python數據科學git
pandas有些功能很逆天,但卻不爲人知,本篇給你們盤點一下。正則表達式
pandas有一種功能很是強大的方法,它就是accessor,能夠將它理解爲一種屬性接口,經過它能夠得到額外的方法。其實這樣說仍是很籠統,下面咱們經過代碼和實例來理解一下。json
>>> pd.Series._accessors {'cat', 'str', 'dt'}
對於Series數據結構使用_accessors方法,能夠獲得了3個對象:cat,str,dt。api
.cat
:用於分類數據(Categorical data).str
:用於字符數據(String Object data).dt
:用於時間數據(datetime-like data)下面咱們依次看一下這三個對象是如何使用的。緩存
str對象的使用微信
Series數據類型:str字符串數據結構
>>> addr = pd.Series([ ... 'Washington, D.C. 20003', ... 'Brooklyn, NY 11211-1755', ... 'Omaha, NE 68154', ... 'Pittsburgh, PA 15211' ... ]) >>> addr.str.upper() 0 WASHINGTON, D.C. 20003 1 BROOKLYN, NY 11211-1755 2 OMAHA, NE 68154 3 PITTSBURGH, PA 15211 dtype: object >>> addr.str.count(r'\d') 0 5 1 9 2 5 3 5 dtype: int64
關於以上str對象的2個方法說明:app
Series.str.upper
:將Series中全部字符串變爲大寫Series.str.count
:對Series中全部字符串的個數進行計數其實不難發現,該用法的使用與Python中字符串的操做很類似。沒錯,在pandas中你同樣能夠這樣簡單的操做,而不一樣的是你操做的是一整列的字符串數據。仍然基於以上數據集,再看它的另外一個操做:dom
>>> regex = (r'(?P<city>[A-Za-z ]+), ' # 一個或更多字母 ... r'(?P<state>[A-Z]{2}) ' # 兩個大寫字母 ... r'(?P<zip>\d{5}(?:-\d{4})?)') # 可選的4個延伸數字 ... >>> addr.str.replace('.', '').str.extract(regex) city state zip 0 Washington DC 20003 1 Brooklyn NY 11211-1755 2 Omaha NE 68154 3 Pittsburgh PA 15211
關於以上str對象的2個方法說明:機器學習
Series.str.replace
:將Series中指定字符串替換Series.str.extract
:經過正則表達式提取字符串中的數據信息這個用法就有點複雜了,由於很明顯看到,這是一個鏈式的用法。經過replace
將 " . "
替換爲""
,即爲空,緊接着又使用了3個正則表達式(分別對應city,state,zip)經過extract
對數據進行了提取,並由原來的Series數據結構變爲了DataFrame數據結構。
固然,除了以上用法外,經常使用的屬性和方法還有.rstrip
,.contains
,split
等,咱們經過下面代碼查看一下str
屬性的完整列表:
>>> [i for i in dir(pd.Series.str) if not i.startswith('_')] ['capitalize', 'cat', 'center', 'contains', 'count', 'decode', 'encode', 'endswith', 'extract', 'extractall', 'find', 'findall', 'get', 'get_dummies', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'islower', 'isnumeric', 'isspace', 'istitle', 'isupper', 'join', 'len', 'ljust', 'lower', 'lstrip', 'match', 'normalize', 'pad', 'partition', 'repeat', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'slice', 'slice_replace', 'split', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'wrap', 'zfill']
屬性有不少,對於具體的用法,若是感興趣能夠本身進行摸索練習。
dt對象的使用
Series數據類型:datetime
由於數據須要datetime類型,因此下面使用pandas的date_range()生成了一組日期datetime演示如何進行dt對象操做。
>>> daterng = pd.Series(pd.date_range('2017', periods=9, freq='Q')) >>> daterng 0 2017-03-31 1 2017-06-30 2 2017-09-30 3 2017-12-31 4 2018-03-31 5 2018-06-30 6 2018-09-30 7 2018-12-31 8 2019-03-31 dtype: datetime64[ns] >>> daterng.dt.day_name() 0 Friday 1 Friday 2 Saturday 3 Sunday 4 Saturday 5 Saturday 6 Sunday 7 Monday 8 Sunday dtype: object >>> # 查看下半年 >>> daterng\[daterng.dt.quarter > 2] 2 2017-09-30 3 2017-12-31 6 2018-09-30 7 2018-12-31 dtype: datetime64[ns] >>> daterng[daterng.dt.is_year_end] 3 2017-12-31 7 2018-12-31 dtype: datetime64[ns]
以上關於dt的3種方法說明:
Series.dt.day_name()
:從日期判斷出所處星期數Series.dt.quarter
:從日期判斷所處季節Series.dt.is_year_end
:從日期判斷是否處在年末其它方法也都是基於datetime的一些變換,並經過變換來查看具體微觀或者宏觀日期。
cat對象的使用
Series數據類型:Category
在說cat對象的使用前,先說一下Category
這個數據類型,它的做用很強大。雖然咱們沒有常常性的在內存中運行上g的數據,可是咱們也總會遇到執行幾行代碼會等待好久的狀況。使用Category
數據的一個好處就是:能夠很好的節省在時間和空間的消耗。下面咱們經過幾個實例來學習一下。
>>> colors = pd.Series([ ... 'periwinkle', ... 'mint green', ... 'burnt orange', ... 'periwinkle', ... 'burnt orange', ... 'rose', ... 'rose', ... 'mint green', ... 'rose', ... 'navy' ... ]) ... >>> import sys >>> colors.apply(sys.getsizeof) 0 59 1 59 2 61 3 59 4 61 5 53 6 53 7 59 8 53 9 53 dtype: int64
上面咱們經過使用sys.getsizeof
來顯示內存佔用的狀況,數字表明字節數。還有另外一種計算內容佔用的方法:memory\_usage()
,後面會使用。
如今咱們將上面colors
的不重複值映射爲一組整數,而後再看一下佔用的內存。
>>> mapper = {v: k for k, v in enumerate(colors.unique())} >>> mapper {'periwinkle': 0, 'mint green': 1, 'burnt orange': 2, 'rose': 3, 'navy': 4} >>> as_int = colors.map(mapper) >>> as_int 0 0 1 1 2 2 3 0 4 2 5 3 6 3 7 1 8 3 9 4 dtype: int64 >>> as_int.apply(sys.getsizeof) 0 24 1 28 2 28 3 24 4 28 5 28 6 28 7 28 8 28 9 28 dtype: int64
注:對於以上的整數值映射也可使用更簡單的
pd.factorize()
方法代替。
咱們發現上面所佔用的內存是使用object類型時的一半。其實,這種狀況就相似於Category data
類型內部的原理。
內存佔用區別:Categorical所佔用的內存與Categorical分類的數量和數據的長度成正比,相反,object所佔用的內存則是一個常數乘以數據的長度。
下面是object
內存使用和category內存使用的狀況對比。
>>> colors.memory_usage(index=False, deep=True) 650 >>> colors.astype('category').memory_usage(index=False, deep=True) 495
上面結果是使用object
和Category
兩種狀況下內存的佔用狀況。咱們發現效果並無咱們想象中的那麼好。可是注意Category內存是成比例的,若是數據集的數據量很大,但不重複分類(unique)值不多的狀況下,那麼Category的內存佔用能夠節省達到10倍以上,好比下面數據量增大的狀況:
>>> manycolors = colors.repeat(10) >>> len(manycolors)/manycolors.nunique() 20.0 >>> manycolors.memory_usage(index=False, deep=True) 6500 >>> manycolors.astype('category').memory_usage(index=False, deep=True) 585
能夠看到,在數據量增長10倍之後,使用Category
所佔內容節省了10倍以上。
除了佔用內存節省外,另外一個額外的好處是計算效率有了很大的提高。由於對於Category
類型的Series
,str字符的操做發生在.cat.categories
的非重複值上,而並不是原Series上的全部元素上。也就是說對於每一個非重複值都只作一次操做,而後再向與非重複值同類的值映射過去。
對於Category
的數據類型,可使用accesso
r的cat對象,以及相應的屬性和方法來操做Category
數據。
>>> ccolors = colors.astype('category') >>> ccolors.cat.categories Index(['burnt orange', 'mint green', 'navy', 'periwinkle', 'rose'], dtype='object')
實際上,對於開始的整數類型映射,能夠先經過reorder_categories
進行從新排序,而後再使用cat.codes
來實現對整數的映射,來達到一樣的效果。
>>> ccolors.cat.reorder_categories(mapper).cat.codes 0 0 1 1 2 2 3 0 4 2 5 3 6 3 7 1 8 3 9 4 dtype: int8
dtype類型是Numpy的int8(-127~128)
。能夠看出以上只須要一個單字節就能夠在內存中包含全部的值。咱們開始的作法默認使用了int64
類型,然而經過pandas
的使用能夠很智能的將Category
數據類型變爲最小的類型。
讓咱們來看一下cat還有什麼其它的屬性和方法可使用。下面cat的這些屬性基本都是關於查看和操做Category數據類型的。
>>> [i for i in dir(ccolors.cat) if not i.startswith('_')] ['add_categories', 'as_ordered', 'as_unordered', 'categories', 'codes', 'ordered', 'remove_categories', 'remove_unused\_categories', 'rename_categories', 'reorder_categories', 'set_categories']
可是Category數據的使用不是很靈活。例如,插入一個以前沒有的值,首先須要將這個值添加到.categories的容器中,而後再添加值。
>>> ccolors.iloc[5] = 'a new color' # ... ValueError: Cannot setitem on a Categorical with a new category, set the categories first >>> ccolors = ccolors.cat.add\_categories(['a new color']) >>> ccolors.iloc[5] = 'a new color'
若是你想設置值或重塑數據,而非進行新的運算操做,那麼Category類型不是那麼有用。
當咱們的數據存在excel表裏,或者其它的IDE編輯器中的時候,咱們想要經過pandas載入數據。咱們一般的作法是先保存再載入,其實這樣作起來十分繁瑣。一個簡單的方法就是使用pd.read\_clipboard()
直接從電腦的剪切板緩存區中提取數據。
這樣咱們就能夠直接將結構數據轉變爲DataFrame或者Series了。excel表中數據是這樣的:
在純文本文件中,好比txt文件,是這樣的:
a b c d 0 1 inf 1/1/00 2 7.389056099 N/A 5-Jan-13 4 54.59815003 nan 7/24/18 6 403.4287935 None NaT
將上面excel或者txt中的數據選中而後複製,而後使用pandas的read_clipboard()
便可完成到DataFrame的轉換。parse_dates
參數設置爲"d",能夠自動識別日期,並調整爲xxxx-xx-xx
的格式。
>>> df = pd.read_clipboard(na_values=[None], parse_dates=['d']) >>> df a b c d 0 0 1.0000 inf 2000-01-01 1 2 7.3891 NaN 2013-01-05 2 4 54.5982 NaN 2018-07-24 3 6 403.4288 NaN NaT >>> df.dtypes a int64 b float64 c float64 d datetime64[ns] dtype: object
在pandas中,咱們能夠直接將objects
打包成爲gzip
, bz2
, zip
, or xz
等壓縮格式,而沒必要將沒壓縮的文件放在內存中而後進行轉化。來看一個例子如何使用:
>>> abalone = pd.read_csv(url, usecols=[0, 1, 2, 3, 4, 8], names=cols) >>> abalone sex length diam height weight rings 0 M 0.455 0.365 0.095 0.5140 15 1 M 0.350 0.265 0.090 0.2255 7 2 F 0.530 0.420 0.135 0.6770 9 3 M 0.440 0.365 0.125 0.5160 10 4 I 0.330 0.255 0.080 0.2050 7 5 I 0.425 0.300 0.095 0.3515 8 6 F 0.530 0.415 0.150 0.7775 20 ... .. ... ... ... ... ... 4170 M 0.550 0.430 0.130 0.8395 10 4171 M 0.560 0.430 0.155 0.8675 8 4172 F 0.565 0.450 0.165 0.8870 11 4173 M 0.590 0.440 0.135 0.9660 10 4174 M 0.600 0.475 0.205 1.1760 9 4175 F 0.625 0.485 0.150 1.0945 10 4176 M 0.710 0.555 0.195 1.9485 12
導入文件,讀取並存爲abalone
(DataFrame結構)。當咱們要存爲壓縮的時候,簡單的使用 to_json()
便可輕鬆完成轉化過程。下面經過設置相應參數將abalone
存爲了.gz
格式的壓縮文件。
abalone.to_json('df.json.gz', orient='records', lines=True, compression='gzip')
若是咱們想知道儲存壓縮文件的大小,能夠經過內置模塊os.path,使用getsize方法來查看文件的字節數。下面是兩種格式儲存文件的大小對比。
>>> import os.path >>> abalone.to_json('df.json', orient='records', lines=True) >>> os.path.getsize('df.json') / os.path.getsize('df.json.gz') 11.603035760226396
在pandas中,有一個測試模塊能夠幫助咱們生成半真實(僞數據),並進行測試,它就是util.testing
。下面同咱們經過一個簡單的例子看一下如何生成數據測試:
>>> import pandas.util.testing as tm >>> tm.N, tm.K = 15, 3 # 默認的行和列 >>> import numpy as np >>> np.random.seed(444) >>> tm.makeTimeDataFrame(freq='M').head() A B C 2000-01-31 0.3574 -0.8804 0.2669 2000-02-29 0.3775 0.1526 -0.4803 2000-03-31 1.3823 0.2503 0.3008 2000-04-30 1.1755 0.0785 -0.1791 2000-05-31 -0.9393 -0.9039 1.1837 >>> tm.makeDataFrame().head() A B C nTLGGTiRHF -0.6228 0.6459 0.1251 WPBRn9jtsR -0.3187 -0.8091 1.1501 7B3wWfvuDA -1.9872 -1.0795 0.2987 yJ0BTjehH1 0.8802 0.7403 -1.2154 0luaYUYvy1 -0.9320 1.2912 -0.2907
上面簡單的使用了
makeTimeDataFrame
和 makeDataFrame
分別生成了一組時間數據和DataFrame的數據。但這只是其中的兩個用法,關於testing
中的方法有大概30多個,若是你想所有了解,能夠經過查看dir得到:
>>> [i for i in dir(tm) if i.startswith('make')] ['makeBoolIndex', 'makeCategoricalIndex', 'makeCustomDataframe', 'makeCustomIndex', # ..., 'makeTimeSeries', 'makeTimedeltaIndex', 'makeUIntIndex', 'makeUnicodeIndex']
也許咱們有的時候會遇到這樣的情形(爲了說明這種情狀況,我使用了product進行交叉迭代的建立了一組關於時間的數據):
>>> from itertools import product >>> datecols = ['year', 'month', 'day'] >>> df = pd.DataFrame(list(product([2017, 2016], [1, 2], [1, 2, 3])), ... columns=datecols) >>> df['data'] = np.random.randn(len(df)) >>> df year month day data 0 2017 1 1 -0.0767 1 2017 1 2 -1.2798 2 2017 1 3 0.4032 3 2017 2 1 1.2377 4 2017 2 2 -0.2060 5 2017 2 3 0.6187 6 2016 1 1 2.3786 7 2016 1 2 -0.4730 8 2016 1 3 -2.1505 9 2016 2 1 -0.6340 10 2016 2 2 0.7964 11 2016 2 3 0.0005
明顯看到,列項中有year,month,day,它們分別在各個列中,而並不是是一個完整日期。那麼如何從這些列中將它們組合在一塊兒並設置爲新的index
呢?
經過to_datetime
的使用,咱們就能夠直接將年月日組合爲一個完整的日期,而後賦給索引。代碼以下:
>>> df.index = pd.to_datetime(df[datecols]) >>> df.head() year month day data 2017-01-01 2017 1 1 -0.0767 2017-01-02 2017 1 2 -1.2798 2017-01-03 2017 1 3 0.4032 2017-02-01 2017 2 1 1.2377 2017-02-02 2017 2 2 -0.2060
固然,你能夠選擇將原有的年月日列移除,只保留data數據列,而後squeeze
轉換爲Series結構。
>>> df = df.drop(datecols, axis=1).squeeze() >>> df.head() 2017-01-01 -0.0767 2017-01-02 -1.2798 2017-01-03 0.4032 2017-02-01 1.2377 2017-02-02 -0.2060 Name: data, dtype: float64 >>> df.index.dtype_str 'datetime64[ns]
歡迎關注微信公衆號Python數據科學,分享機器學習、數據挖掘、Python數據分析、爬蟲等乾貨,同時可免費得到百G學習資源,期待咱們一塊兒學習。