系列(Series)是可以保存任何類型的數據(整數,字符串,浮點數,Python對象等)的一維標記數組。軸標籤統稱爲索引python
構造函數數組
pandas.Series(data, index, dtype, copy)
編號 | 參數 | 描述 |
---|---|---|
1 | data | 數據採起各類形式,如:ndarray,list,constants |
2 | index | 索引值必須是惟一的和散列的,與數據的長度相同 默認np.arange(n)若是沒有索引被傳遞 |
3 | dtype | dtype用於數據類型 若是沒有,將推斷數據類型 |
4 | copy | 複製數據,默認爲false |
構成一個Series的輸入有:數據結構
#import the pandas library and aliasing as pd import pandas as pd import numpy as np data = np.array(['a','b','c','d']) s = pd.Series(data,index=[100,101,102,103]) print s
100 a 101 b 102 c 103 d dtype: object
#import the pandas library and aliasing as pd import pandas as pd import numpy as np data = {'a' : 0., 'b' : 1., 'c' : 2.} s = pd.Series(data,index=['b','c','d','a']) print s
b 1.0 c 2.0 d NaN a 0.0 dtype: float64
#import the pandas library and aliasing as pd import pandas as pd import numpy as np s = pd.Series(5, index=[0, 1, 2, 3]) print s
0 5 1 5 2 5 3 5 dtype: int64
數據幀(DataFrame)是二維數據結構,即數據以行和列的表格方式排列app
數據幀(DataFrame)的功能特色:dom
構造函數:函數
pandas.DataFrame(data, index, columns, dtype, copy)
編號 | 參數 | 描述 |
---|---|---|
1 | data | 數據採起各類形式,如:ndarray,series,map,lists,dict,constant和另外一個DataFrame。 |
2 | index | 對於行標籤,要用於結果幀的索引是可選缺省值np.arrange(n),若是沒有傳遞索引值。 |
3 | columns | 對於列標籤,可選的默認語法是 - np.arange(n)。 這隻有在沒有索引傳遞的狀況下才是這樣。 |
4 | dtype | 每列的數據類型。 |
5 | copy | 若是默認值爲False,則此命令(或任何它)用於複製數據。 |
Pandas數據幀(DataFrame)可使用各類輸入建立post
import pandas as pd data = [1,2,3,4,5] df = pd.DataFrame(data) print df
res:spa
0 0 1 1 2 2 3 3 4 4 5
import pandas as pd data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] df = pd.DataFrame(data, index=['first', 'second']) print df
res:3d
a b c 0 1 2 NaN 1 5 10 20.0
import pandas as pd data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]} df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4']) print df
res:code
Age Name rank1 28 Tom rank2 34 Jack rank3 29 Steve rank4 42 Ricky
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) print df
res:
one two a 1.0 1 b 2.0 2 c 3.0 3 d NaN 4
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) print df ['one']
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) # Adding a new column to an existing DataFrame object with column label by passing new series print ("Adding a new column by passing as Series:") df['three']=pd.Series([10,20,30],index=['a','b','c']) print df print ("Adding a new column using the existing columns in DataFrame:") df['four']=df['one']+df['three'] print df
# Using the previous DataFrame, we will delete a column # using del function import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']), 'three' : pd.Series([10,20,30], index=['a','b','c'])} df = pd.DataFrame(d) print ("Our dataframe is:") print df # using del function print ("Deleting the first column using DEL function:") del df['one'] print df # using pop function print ("Deleting another column using POP function:") df.pop('two') print df
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) print df.loc['b']
import pandas as pd d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])} df = pd.DataFrame(d) print df.iloc[2]
使用append()函數將新行添加到DataFrame
import pandas as pd df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b']) df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b']) df = df.append(df2) print df
使用索引標籤從DataFrame中刪除或刪除行。 若是標籤重複,則會刪除多行。
import pandas as pd df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b']) df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b']) df = df.append(df2) # Drop rows with label 0 df = df.drop(0) print df
面板(Panel)是3D容器的數據
3軸(axis)這個名稱旨在給出描述涉及面板數據的操做的一些語義
軸 | details |
---|---|
items | axis 0,每一個項目對應於內部包含的數據幀(DataFrame) |
major_axis | axis 1,它是每一個數據幀(DataFrame)的索引(行) |
minor_axis | axis 2,它是每一個數據幀(DataFrame)的列 |
pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)
構造函數的參數以下:
參數 | 描述 |
---|---|
data | 數據採起各類形式,如:ndarray,series,map,lists,dict,constant和另外一個數據幀(DataFrame) |
items | axis=0 |
major_axis | axis=1 |
minor_axis | axis=2 |
dtype | 每列的數據類型 |
copy | 複製數據,默認 - false |
可使用多種方式建立面板
# creating an empty panel import pandas as pd import numpy as np data = np.random.rand(2,4,5) p = pd.Panel(data) print data print p
res:
>>> print data [[[0.79346549 0.22729079 0.94261176 0.67379434 0.18751374] [0.14514546 0.50550601 0.32767807 0.45882726 0.04787695] [0.64748544 0.2019516 0.38334503 0.61874107 0.68800838] [0.39880845 0.41415895 0.69383131 0.71159435 0.06160828]] [[0.97102379 0.69454937 0.54629548 0.83072134 0.53068539] [0.82441684 0.5882186 0.69936055 0.0924247 0.12300041] [0.30401452 0.12971053 0.90511636 0.17855185 0.05474733] [0.04730471 0.03639553 0.74632198 0.85193736 0.64864719]]] >>> print p <class 'pandas.core.panel.Panel'> Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis) Items axis: 0 to 1 Major_axis axis: 0 to 3 Minor_axis axis: 0 to 4
#creating an empty panel import pandas as pd import numpy as np data = {'Item1' : pd.DataFrame(np.random.randn(4, 3)), 'Item2' : pd.DataFrame(np.random.randn(4, 2))} p = pd.Panel(data) print data print p
res:
{'Item2': 0 1 0 0.009730 2.263936 1 -1.008878 1.083319 2 0.288527 0.234344 3 -0.426486 0.286741, 'Item1': 0 1 2 0 -2.149956 1.696135 -0.256530 1 -1.063944 -1.033069 0.653613 2 -0.645782 -0.097129 1.034462 3 -0.041070 0.104719 0.577797} >>> print p <class 'pandas.core.panel.Panel'> Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis) Items axis: Item1 to Item2 Major_axis axis: 0 to 3 Minor_axis axis: 0 to 2
#creating an empty panel import pandas as pd p = pd.Panel() print p
res:
<class 'pandas.core.panel.Panel'> Dimensions: 0 (items) x 0 (major_axis) x 0 (minor_axis) Items axis: None Major_axis axis: None Minor_axis axis: None
要從面板中選擇數據,可使用如下方式
import pandas as pd import numpy as np data = {'Item1' : pd.DataFrame(np.arange(12).reshape(4, 3)), 'Item2' : pd.DataFrame(np.arange(8).reshape(4, 2))} p = pd.Panel(data) print data # 使用Item print p['Item1'] # 使用Major_axis print p.major_xs(1) # 使用Minor_axis print p.minor_xs(1)
data: {'Item2': 0 1 0 0 1 1 2 3 2 4 5 3 6 7, 'Item1': 0 1 2 0 0 1 2 1 3 4 5 2 6 7 8 3 9 10 11} >>> # 使用Item ... print p['Item1'] 0 1 2 0 0 1 2 1 3 4 5 2 6 7 8 3 9 10 11 >>> # 使用Major_axis ... print p.major_xs(1) Item1 Item2 0 3 2.0 1 4 3.0 2 5 NaN >>> # 使用Minor_axis ... print p.minor_xs(1) Item1 Item2 0 1 1.0 1 4 3.0 2 7 5.0 3 10 7.0