>>> pd.Series._accessors
{'cat', 'str', 'dt'}對於Series數據結構使用_accessors方法,我們得到了3個對象:cat,str,dt。
.cat:用於分類數據(Categorical data).str:用於字符數據(String Object data).dt:用於時間數據(datetime-like data)下面我們依次看一下這三個對象是如何使用的。
str對象的使用Series數據類型:str字符串
# 定義一個Series序列
>>> 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個方法說明:
Series.str.upper:將Series中所有字符串變為大寫;Series.str.count:對Series中所有字符串的個數進行計數;其實不難發現,該用法的使用與Python中字符串的操作很相似。沒錯,在pandas中你一樣可以這樣簡單的操作,而不同的是你操作的是一整列的字符串數據。仍然基於以上數據集,再看它的另一個操作:
>>> 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的數據類型,可以使用accessor的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: int8dtype類型是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類型不是那麼有用。