原文鏈接:blog.ouyangsihai.cn >> pandas教程:series和dataframe
起步
pandas是一種Python數(shù)據(jù)分析的利器,是一個(gè)開源的數(shù)據(jù)分析包型宙,最初是應(yīng)用于金融數(shù)據(jù)分析工具而開發(fā)出來的电抚,因此pandas為時(shí)間序列分析提供了很好的支持涵防。pandas是PyData項(xiàng)目的一部分。
官網(wǎng):http://pandas.pydata.org/
官方文檔:http://pandas.pydata.org/pandas-docs/stable/
安裝與導(dǎo)入
安裝方式
Python的Anaconda發(fā)行版,已經(jīng)安裝好pandas庫歉提,不需要另外安裝
使用Anaconda界面安裝态兴,選擇對(duì)應(yīng)的pandas進(jìn)行勾選安裝即可
使用Anaconda命令安裝:conda install pandas
使用PyPi安裝命令安裝:pip install pandas
導(dǎo)入:
from pandas import Series, DataFrame
import pandas as pd
Pandas的數(shù)據(jù)類型
Pandas基于兩種數(shù)據(jù)類型: series 與 dataframe 狠持。
Series:一種類似于一維數(shù)組的對(duì)象,是由一組數(shù)據(jù)(各種NumPy數(shù)據(jù)類型)以及一組與之相關(guān)的數(shù)據(jù)標(biāo)簽(即索引)組成瞻润。僅由一組數(shù)據(jù)也可產(chǎn)生簡單的Series對(duì)象喘垂。注意:Series中的索引值是可以重復(fù)的。
DataFrame:一個(gè)表格型的數(shù)據(jù)結(jié)構(gòu)绍撞,包含有一組有序的列正勒,每列可以是不同的值類型(數(shù)值、字符串傻铣、布爾型等)章贞,DataFrame即有行索引也有列索引,可以被看做是由Series組成的字典非洲。
Series
一個(gè)series是一個(gè)一維的數(shù)據(jù)類型鸭限,其中每一個(gè)元素都有一個(gè)標(biāo)簽。類似于Numpy中元素帶標(biāo)簽的數(shù)組两踏。其中败京,標(biāo)簽可以是數(shù)字或者字符串。
- series屬性
編號(hào) | 屬性或方法 | 描述 |
---|---|---|
1 | axes | 返回行軸標(biāo)簽列表梦染。 |
2 | dtype | 返回對(duì)象的數(shù)據(jù)類型(dtype)赡麦。 |
3 | empty | 如果系列為空,則返回True。 |
4 | ndim | 返回底層數(shù)據(jù)的維數(shù)隧甚,默認(rèn)定義:1车荔。 |
5 | size | 返回基礎(chǔ)數(shù)據(jù)中的元素?cái)?shù)。 |
6 | values | 將系列作為ndarray返回戚扳。 |
7 | head() | 返回前n行忧便。 |
8 | tail() | 返回最后n行。 |
- pandas.Series( data, index, dtype, copy)
編號(hào) | 參數(shù) | 描述 |
---|---|---|
1 | data | 數(shù)據(jù)采取各種形式帽借,如:ndarray珠增,list,constants |
2 | index | 索引值必須是唯一的和散列的砍艾,與數(shù)據(jù)的長度相同蒂教。 默認(rèn)np.arange(n)如果沒有索引被傳遞。 |
3 | dtype | dtype用于數(shù)據(jù)類型脆荷。如果沒有凝垛,將推斷數(shù)據(jù)類型 |
4 | copy | 復(fù)制數(shù)據(jù),默認(rèn)為false蜓谋。 |
創(chuàng)建series方式
通過一維數(shù)組方式創(chuàng)建
import numpy as np
import pandas as pd
s = pd.Series([1, 2, 5, np.nan, 6, 8])
print(s)
輸出:
0 1.0
1 2.0
2 5.0
3 NaN
4 6.0
5 8.0
dtype: float64
從ndarray創(chuàng)建一個(gè)系列
data = np.array(['a','b','c','d'])
ser02 = pd.Series(data)
ser02
#指定索引
data = np.array(['a','b','c','d'])
# ser02 = pd.Series(data,index=[100,101,102,103])
ser02 = pd.Series(data,index=['name','age','sex','address'])
ser02
輸出:
0 a
1 b
2 c
3 d
dtype: object
name a
age b
sex c
address d
dtype: object
從字典創(chuàng)建一個(gè)系列
字典(dict)可以作為輸入傳遞梦皮,如果沒有指定索引,則按排序順序取得字典鍵以構(gòu)造索引桃焕。 如果傳遞了索引剑肯,索引中與標(biāo)簽對(duì)應(yīng)的數(shù)據(jù)中的值將被拉出。
data = {'a':1,'b':2,'c':3}
ser03 = pd.Series(data)
ser03
#指定索引
data = {'a':1,'b':2,'c':3}
ser03 = pd.Series(data,index = ['a','b','c','d'])
ser03
#標(biāo)量創(chuàng)建
ser04 = pd.Series(5,index = [0,1,2,3])
ser04
輸出:
a 1
b 2
c 3
dtype: int64
a 1.0
b 2.0
c 3.0
d NaN
dtype: float64
0 5
1 5
2 5
3 5
dtype: int64
Series值的獲取
Series值的獲取主要有兩種方式:
- 通過方括號(hào)+索引的方式讀取對(duì)應(yīng)索引的數(shù)據(jù)观堂,有可能返回多條數(shù)據(jù)
- 通過方括號(hào)+下標(biāo)值的方式讀取對(duì)應(yīng)下標(biāo)值的數(shù)據(jù)让网,下標(biāo)值的取值范圍為:[0,len(Series.values))师痕;另外下標(biāo)值也可以是負(fù)數(shù)溃睹,表示從右往左獲取數(shù)據(jù)
Series獲取多個(gè)值的方式類似NumPy中的ndarray的切片操作,通過方括號(hào)+下標(biāo)值/索引值+冒號(hào)(:)的形式來截取series對(duì)象中的一部分?jǐn)?shù)
#引入模塊
import pandas as pd
import numpy as np
#檢索第一個(gè)元素胰坟。
ser05 = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print(ser05[1])
print(ser05['a'])
print(ser05['d'])
輸出:
2
1
4
#檢索系列中的前三個(gè)元素
ser05 = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#通過索引來獲取數(shù)據(jù)
print(ser05[:3])
print(ser05[::2])
print(ser05[4:2:-1])
#通過標(biāo)簽(下標(biāo)值)來獲取數(shù)據(jù)
print(ser05['b':'d'])
ser05['a':'d':2]
ser05['e':'c':-1]
ser05[['a','b']]
輸出:
a 1
b 2
c 3
dtype: int64
a 1
c 3
e 5
dtype: int64
e 5
d 4
dtype: int64
b 2
c 3
d 4
dtype: int64
a 1
b 2
dtype: int64
Series的運(yùn)算
#引入模塊
import pandas as pd
import numpy as np
series = pd.Series({'a':941,'b':431,'c':9327})
series
#輸出大于500的值
series[series>500]
#計(jì)算加
series+10
#計(jì)算減
series-100
#計(jì)算乘
series*10
#兩個(gè)系列相加
ser01 = pd.Series([1,2,3])
ser02 = pd.Series([4,5,6])
ser01+ser02
#)計(jì)算各個(gè)元素的指數(shù)e的x次方 e 約等于 2.71828
np.exp(series)
np.abs(series)
#sign()計(jì)算各個(gè)元素的正負(fù)號(hào): 1 正數(shù)因篇,0:零,-1:負(fù)數(shù)
np.sign(series)
Series自動(dòng)對(duì)齊
當(dāng)多個(gè)series對(duì)象之間進(jìn)行運(yùn)算的時(shí)候腕铸,如果不同series之間具有不同的索引值惜犀,那么運(yùn)算會(huì)自動(dòng)對(duì)齊不同索引值的數(shù)據(jù)铛碑,如果某個(gè)series沒有某個(gè)索引值狠裹,那么最終結(jié)果會(huì)賦值為NaN。
#引入模塊
import pandas as pd
import numpy as np
serA = pd.Series([1,2,3],index = ['a','b','c'])
serB = pd.Series([4,5,6],index = ['b','c','d'])
print('---------serA+serB---------')
print(serA)
serA+serB
輸出:
---------serA+serB---------
a 1
b 2
c 3
dtype: int64
a NaN
b 6.0
c 8.0
d NaN
dtype: float64
Series及其索引的name屬性
Series對(duì)象本身以及索引都具有一個(gè)name屬性汽烦,默認(rèn)為空涛菠,根據(jù)需要可以進(jìn)行賦值操作
DataFrame
一個(gè)dataframe是一個(gè)二維的表結(jié)構(gòu)。Pandas的dataframe可以存儲(chǔ)許多種不同的數(shù)據(jù)類型,并且每一個(gè)坐標(biāo)軸都有自己的標(biāo)簽俗冻。你可以把它想象成一個(gè)series的字典項(xiàng)礁叔。
dataFrame屬性
編號(hào) | 屬性或方法 | 描述 | |
---|---|---|---|
1 | T | 轉(zhuǎn)置行和列。 | |
2 | axes | 返回一個(gè)列迄薄,行軸標(biāo)簽和列軸標(biāo)簽作為唯一的成員 | 琅关。 |
3 | dtypes | 返回此對(duì)象中的數(shù)據(jù)類型(dtypes)。 | |
4 | empty | 如果NDFrame完全為空[無項(xiàng)目]讥蔽,則返回為True; 如果任何軸的長度為0涣易。 | |
5 | ndim | 軸/數(shù)組維度大小。 | |
6 | shape | 返回表示DataFrame的維度的元組冶伞。 | |
7 | size | NDFrame中的元素?cái)?shù)新症。 | |
8 | values | NDFrame的Numpy表示。 | |
9 | head() | 返回開頭前n行响禽。 | |
10 | tail() | 返回最后n行徒爹。 |
dataframe創(chuàng)建方式
pandas中的DataFrame可以使用以下構(gòu)造函數(shù)創(chuàng)建
- pandas.DataFrame( data, index, columns, dtype, copy)
編號(hào) | 參數(shù) | 描述 |
---|---|---|
1 | data | 數(shù)據(jù)采取各種形式,如:ndarray芋类,series隆嗅,map,lists梗肝,dict榛瓮,constant和另一個(gè)DataFrame。 |
2 | index | 對(duì)于行標(biāo)簽巫击,要用于結(jié)果幀的索引是可選缺省值np.arrange(n)禀晓,如果沒有傳遞索引值。 |
3 | columns | 對(duì)于列標(biāo)簽坝锰,可選的默認(rèn)語法是 - np.arange(n)粹懒。 這只有在沒有索引傳遞的情況下才是這樣。 |
4 | dtype | 每列的數(shù)據(jù)類型顷级。 |
5 | copy | 如果默認(rèn)值為False凫乖,則此命令(或任何它)用于復(fù)制數(shù)據(jù)。 |
創(chuàng)建一個(gè) DateFrame:
#創(chuàng)建日期索引序列
dates =pd.date_range('20130101', periods=6)
print(type(dates))
#創(chuàng)建Dataframe弓颈,其中 index 決定索引序列帽芽,columns 決定列名
df =pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
print(df)
輸出:
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>
A B C D
2013-01-01 0.406575 -1.356139 0.188997 -1.308049
2013-01-02 -0.412154 0.123879 0.907458 0.201024
2013-01-03 0.576566 -1.875753 1.967512 -1.044405
2013-01-04 1.116106 -0.796381 0.432589 0.764339
2013-01-05 -1.851676 0.378964 -0.282481 0.296629
2013-01-06 -1.051984 0.960433 -1.313190 -0.093666
字典創(chuàng)建 DataFrame
df2 =pd.DataFrame({'A' : 1.,
'B': pd.Timestamp('20130102'),
'C': pd.Series(1,index=list(range(4)),dtype='float32'),
'D': np.array([3]*4,dtype='int32'),
'E': pd.Categorical(["test","train","test","train"]),
'F':'foo' })
print(df2)
輸出:
A B C D E F
0 1.0 2013-01-02 1.0 3 test foo
1 1.0 2013-01-02 1.0 3 train foo
2 1.0 2013-01-02 1.0 3 test foo
3 1.0 2013-01-02 1.0 3 train foo
從列表創(chuàng)建DataFrame
data = [1,2,3,4]
df02 = pd.DataFrame(data)
df02
輸出:
0
0 1
1 2
2 3
3 4
從列表字典來創(chuàng)建DataFrame
data = {'Name':['Tom','Jack','Steve'],'Age':[19,18,20]}
# df04 = pd.DataFrame(data)
#指定行索引和列索引
df04 = pd.DataFrame(data,index = ['rank1','rank2','rank3'],columns = ['Name','Age','Sex'])
df04
輸出:
Name Age Sex
rank1 Tom 19 NaN
rank2 Jack 18 NaN
rank3 Steve 20 NaN
從字典列表創(chuàng)建數(shù)據(jù)幀DataFrame
data = [{'a':1,'b':2},{'a':1,'b':2,'c':3}]
# df05 = pd.DataFrame(data)
#傳遞字典列表指定行索引
# df05 = pd.DataFrame(data,index = ['first','second'])
#傳遞字典列表指定行索引,列索引
df05 = pd.DataFrame(data,index = ['first','second'],columns = ['a','b','c','d'])
df05
輸出:
a b c d
first 1 2 NaN NaN
second 1 2 3.0 NaN
從系列的字典來創(chuàng)建DataFrame
data = {
'one':pd.Series([1,2,3],index = ['a','b','c']),
'two':pd.Series([1,2,3,4],index = ['a','b','c','d'])
}
df06 = pd.DataFrame(data)
df06
輸出:
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4
dataFrame數(shù)據(jù)操作
列選擇
#直接通過列索引來獲取某一列的值
data = {
'one':pd.Series([1,2,3],index = ['a','b','c']),
'two':pd.Series([1,2,3,4],index = ['a','b','c','d'])
}
df06 = pd.DataFrame(data)
df06
df06['one']
# df06.one
# df06.ix[:,'one']
# df06.loc[:,'one']
# df06.iloc[:,0]
列添加
data = {
'one':pd.Series([1,2,3],index = ['a','b','c']),
'two':pd.Series([1,2,3,4],index = ['a','b','c','d'])
}
df06 = pd.DataFrame(data)
df06['three'] = pd.Series([10,20,30],index = ['a','b','c'])
df06
列修改
#直接通過列名進(jìn)行修改
df06['three'] = [7,8,9,10]
df06
列刪除
data = {
'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'])
}
df06 = pd.DataFrame(data)
#使用del刪除列
# del(df06['three'])
#使用pop刪除
df06.pop('two')
df06
行選擇
data = {
'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'])
}
df06 = pd.DataFrame(data)
df06
#可以通過將行標(biāo)簽傳遞給loc函數(shù)或者ix函數(shù)來選擇行
# df06.loc['a']
df06.loc[:,'two']
# df06.ix['a']
# 按整數(shù)位置選擇
# 可以通過將整數(shù)位置傳遞給iloc函數(shù)來選擇行翔冀。參考以下示例代碼 -
df06.iloc[2]
# 行切片
# 可以使用:運(yùn)算符選擇多行导街。參考以下示例代碼 -
df06[2:4]
行添加
# df06.ix['e'] = [22,33,444]
df06.loc['e'] = [22,33,444]
df06
# 添加加行
# 使用append()函數(shù)將新行添加到DataFrame。 此功能將附加行結(jié)束纤子。
#創(chuàng)建一行數(shù)據(jù)
# data2 = pd.DataFrame([{'one':22,'two':33,'three':44}],index = ['e'])
data2 = pd.DataFrame([[22,33,44]],columns = ['one','two','three'],index = ['f'])
# data2
df06 = df06.append(data2)
df06
行刪除
df06 = df06.drop('e')
df06
文章有不當(dāng)之處搬瑰,歡迎指正款票,如果喜歡微信閱讀,你也可以關(guān)注我的微信公眾號(hào):
cplus人工智能算法后端技術(shù)
泽论,獲取優(yōu)質(zhì)學(xué)習(xí)資源艾少。