01Pandas_数据结构
Pandas數(shù)據(jù)結(jié)構(gòu)
做python數(shù)據(jù)分析,數(shù)據(jù)挖掘,機(jī)器學(xué)習(xí)的童鞋應(yīng)該都離不開pandas。在做數(shù)據(jù)的預(yù)處理的時(shí)候pandas尤為給力。
本文主要介紹pandas中的兩種數(shù)據(jù)結(jié)構(gòu):series,dataframe。
import pandas as pd1.Series
首先來(lái)介紹series數(shù)據(jù)結(jié)構(gòu)。
series 類似于一維數(shù)組的對(duì)象。對(duì)于series基本要掌握的是:
1.1 構(gòu)建Series
- 通過(guò)list構(gòu)建Series
向pd.Series()中傳入一個(gè)list。就等于將這個(gè)list轉(zhuǎn)換成了Series數(shù)據(jù)格式了。
可以通過(guò)打印數(shù)據(jù)類型來(lái)檢查,顯示的是Series
ser_obj = pd.Series(range(10, 20))print type(ser_obj) <class 'pandas.core.series.Series'>- 通過(guò)字典dict構(gòu)建Series
dict中每個(gè)key其實(shí)是索引,對(duì)應(yīng)的value是值。所有的值的數(shù)據(jù)類型需一致。
year_data = {2001: 17.8, 2002: 20.1, 2003: 16.5} ser_obj2 = pd.Series(year_data) print ser_obj2.head() 2001 17.8 2002 20.1 2003 16.5 dtype: float641.2 獲取數(shù)據(jù)與索引
對(duì)于Series, 使用.values方法就能獲取它的值;使用.index方法就能獲取它的索引。
下面這個(gè)例子獲取的索引并沒(méi)有直接逐個(gè)打印出來(lái),而是打印了一個(gè)RangeIndex,里面的參數(shù)表示起始數(shù)(包括),結(jié)尾數(shù)(不包括),步長(zhǎng)為1。
# 獲取數(shù)據(jù) print ser_obj.values# 獲取索引 print ser_obj.index [10 11 12 13 14 15 16 17 18 19] RangeIndex(start=0, stop=10, step=1)1.3 預(yù)覽數(shù)據(jù)
如果數(shù)據(jù)量太大,但又想看看數(shù)據(jù)的格式,那么可以提取前幾條數(shù)據(jù)來(lái)瞧一瞧。
直接使用.head(),如果里面不傳入?yún)?shù),那么默認(rèn)提取前5條數(shù)據(jù);括號(hào)里也可以出傳入?yún)?shù)來(lái)指定提取前面n條。
# 預(yù)覽數(shù)據(jù) print ser_obj.head(3) 0 10 1 11 2 12 dtype: int641.4 獲取數(shù)據(jù)
可以通過(guò)索引獲取Series中對(duì)應(yīng)位置的value。索引放在中括號(hào)[]中。
#通過(guò)索引獲取數(shù)據(jù) print ser_obj[0] print ser_obj[8] 10 181.5 運(yùn)算
對(duì)1個(gè)Series 進(jìn)行加減乘數(shù)的運(yùn)算時(shí),表示對(duì)Series中的每個(gè)元素都做一次運(yùn)算,然后輸出相同長(zhǎng)度的Series。
# 索引與數(shù)據(jù)的對(duì)應(yīng)關(guān)系仍保持在數(shù)組運(yùn)算的結(jié)果中 print ser_obj * 3 0 30 1 33 2 36 3 39 4 42 5 45 6 48 7 51 8 54 9 57 dtype: int64除了普通的加減乘除等運(yùn)算,還可以進(jìn)行布爾運(yùn)算,如下,會(huì)將所有大于15的值輸出成True, 小于15的值輸出成False。
print ser_obj > 15 0 False 1 False 2 False 3 False 4 False 5 False 6 True 7 True 8 True 9 True dtype: bool1.6 name屬性
可以對(duì)Series中的Index和Values添加自定義的名字。
# name屬性 ser_obj2.name = 'score' ser_obj2.index.name = 'year' print ser_obj2.head() year 2001 17.8 2002 20.1 2003 16.5 Name: score, dtype: float642.DataFrame
DataFrame類似于多維數(shù)組或表格數(shù)據(jù),與excel類似。
每列數(shù)據(jù)可以是不同的類型,但是同一列的數(shù)據(jù)需保持一致數(shù)據(jù)類型。
DataFrame的索引包括行索引與列索引。
掌握DataFrame的基本使用,需要熟悉以下幾個(gè)要點(diǎn)。
- ?
2.1 構(gòu)建DataFrame
- 通過(guò)ndarray構(gòu)建DataFram
上面構(gòu)建好的DataFrame可見(jiàn)左邊有一列是行索引,上面有一行是列索引。如果沒(méi)有特殊指定,系統(tǒng)會(huì)默認(rèn)生成行索引與列索引的。
- 通過(guò)dict構(gòu)建DataFrame
還記得通過(guò)字典構(gòu)建series時(shí),Key是作為索引的;在DataFrame中,Key是作為列索引(列名)。
講dict傳給pd.DataFrame()中即構(gòu)成了一個(gè)DataFrame
dict_data = {'A': 1., 'B': pd.Timestamp('20161223'),'C': pd.Series(1, index=list(range(4)),dtype='float32'),'D': np.array([3] * 4,dtype='int32'),'E' : pd.Categorical(["Python","Java","C++","C#"]),'F' : 'wangxiaocao' } #print dict_data df_obj2 = pd.DataFrame(dict_data) print df_obj2.head() A B C D E F 0 1.0 2016-12-23 1.0 3 Python wangxiaocao 1 1.0 2016-12-23 1.0 3 Java wangxiaocao 2 1.0 2016-12-23 1.0 3 C++ wangxiaocao 3 1.0 2016-12-23 1.0 3 C# wangxiaocao2.2 通過(guò)索引獲取數(shù)據(jù)
這里先簡(jiǎn)單介紹一下通過(guò)列索引來(lái)獲取數(shù)據(jù)。
通過(guò)列索引獲取的數(shù)據(jù)顧名思義就是獲取處該索引的一整列。著一整列的數(shù)據(jù)其實(shí)就是Series的數(shù)據(jù)格式。
所以DataFrame可以看成是由一列一列的series組成的。
有兩種方式:
1. df_obj2[‘F’]
2. df_obj2.F
2.3 增加與刪除列
# 增加列 df_obj2['G'] = df_obj2['D'] + 4 print df_obj2.head() A B C D E F G 0 1.0 2016-12-23 1.0 3 Python wangxiaocao 7 1 1.0 2016-12-23 1.0 3 Java wangxiaocao 7 2 1.0 2016-12-23 1.0 3 C++ wangxiaocao 7 3 1.0 2016-12-23 1.0 3 C# wangxiaocao 7 # 刪除列 del df_obj2['G'] print df_obj2.head() A B C D E F 0 1.0 2016-12-23 1.0 3 Python wangxiaocao 1 1.0 2016-12-23 1.0 3 Java wangxiaocao 2 1.0 2016-12-23 1.0 3 C++ wangxiaocao 3 1.0 2016-12-23 1.0 3 C# wangxiaocao3.索引對(duì)象 Index
pandas的兩種數(shù)據(jù)格式都與索引息息相關(guān),這里羅列一下索引的相關(guān)知識(shí)。
首先要明確索引的特性:不可變!索引
# 索引對(duì)象不可變 df_obj2.index[0] = 2 ---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-17-7f40a356d7d1> in <module>()1 # 索引對(duì)象不可變 ----> 2 df_obj2.index[0] = 2/home/cc/anaconda2/lib/python2.7/site-packages/pandas/indexes/base.pyc in __setitem__(self, key, value)1243 1244 def __setitem__(self, key, value): -> 1245 raise TypeError("Index does not support mutable operations")1246 1247 def __getitem__(self, key):TypeError: Index does not support mutable operations常見(jiàn)的Index種類有:
注:部分例子來(lái)自于小象學(xué)院Robin課程
總結(jié)
以上是生活随笔為你收集整理的01Pandas_数据结构的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 华为身处“创新者的窘境”,而浑然不觉(转
- 下一篇: 【解决】jupyter在deepin安装