pandas入門:Series、DataFrame、Index基本操作都有了!

導(dǎo)讀:pandas是一款開放源碼的BSD許可的Python庫。它基于NumPy創(chuàng)建,為Python編程語言提供了高性能的、易于使用的數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)分析工具。
作者:李明江 張良均 周東平 張尚佳
來源:大數(shù)據(jù)DT(ID:hzdashuju)

Series:基本數(shù)據(jù)結(jié)構(gòu),一維標(biāo)簽數(shù)組,能夠保存任何數(shù)據(jù)類型 DataFrame:基本數(shù)據(jù)結(jié)構(gòu),一般為二維數(shù)組,是一組有序的列 Index:索引對(duì)象,負(fù)責(zé)管理軸標(biāo)簽和其他元數(shù)據(jù)(比如軸名稱) groupby:分組對(duì)象,通過傳入需要分組的參數(shù)實(shí)現(xiàn)對(duì)數(shù)據(jù)分組 Timestamp:時(shí)間戳對(duì)象,表示時(shí)間軸上的一個(gè)時(shí)刻 Timedelta:時(shí)間差對(duì)象,用來計(jì)算兩個(gè)時(shí)間點(diǎn)的差值
class?pandas.Series(data=None,?index=None,?dtype=None,?name=None,?copy=False,?fastpath=False)
data:接收array或dict。表示接收的數(shù)據(jù)。默認(rèn)為None index:接收array或list。表示索引,它必須與數(shù)據(jù)長(zhǎng)度相同。默認(rèn)為None name:接收string或list。表示Series對(duì)象的名稱。默認(rèn)為None
代碼清單6-1 通過ndarray創(chuàng)建Series
import?pandas?as?pd
import?numpy?as?np
print('通過ndarray創(chuàng)建的Series為:\n',
??????pd.Series(np.arange(5),?index?=?['a',?'b',?'c',?'d',?'e'],?name?=?'ndarray'))
輸出:
通過ndarray創(chuàng)建的Series為:
a????0
b????1
c????2
d????3
e????4
Name:?ndarray,?dtype:?int32
代碼清單6-2 通過dict創(chuàng)建Series
dit?=?{'a':?0,?'b':?1,?'c':?2,?'d':?3,?'e':?4}
print('通過dict創(chuàng)建的Series為:\n',?pd.Series(dit))
通過dict創(chuàng)建的Series為:
a????0
b????1
c????2
d????3
e????4
dtype:?int64代碼清單6-3 通過list創(chuàng)建Series
list1?=?[0,?1,?2,?3,?4]
print('通過list創(chuàng)建的Series為:\n',?pd.Series(list1,?index?=?['a',?'b',?'c',?'d',?'e'],?name?=?'list'))
通過list創(chuàng)建的Series為:
a????0
b????1
c????2
d????3
e????4
Name:?list,?dtype:?int64values:以ndarray的格式返回Series對(duì)象的所有元素 index:返回Series對(duì)象的索引 dtype:返回Series對(duì)象的數(shù)據(jù)類型 shape:返回Series對(duì)象的形狀 nbytes:返回Series對(duì)象的字節(jié)數(shù) ndim:返回Series對(duì)象的維度 size:返回Series對(duì)象的個(gè)數(shù) T:返回Series對(duì)象的轉(zhuǎn)置
代碼清單6-4 訪問Series的屬性
series?=?pd.Series(list1,?index?=?['a',?'b',?'c',?'d',?'e'],?name?=?'list')
print('數(shù)組形式返回Series為:',?series.values)
#輸出:數(shù)組形式返回Series為:?[0 1 2 3 4]
print('Series的Index為:',?series.index)
#輸出:Series的Index為:Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
print('Series的形狀為:',?series.shape)
#輸出:Series的形狀為:?(5,)
print('Series的維度為:',?series.ndim)
#輸出:Series的維度為:1
代碼清單6-5 通過索引位置訪問Series數(shù)據(jù)子集
print('Series位于第1位置的數(shù)據(jù)為:',?series[0])
Series位于第1位置的數(shù)據(jù)為:?0代碼清單6-6 通過索引名稱訪問Series數(shù)據(jù)
print('Series中Index為a的數(shù)據(jù)為:',?series['a'])
Series中Index為a的數(shù)據(jù)為:?0代碼清單6-7 通過bool數(shù)組訪問Series數(shù)據(jù)
bool?=?(series?4)
print('bool類型的Series為:\n',?bool)
輸出:
bool類型的Series為:
a?????True
b?????True
c?????True
d?????True
e????False
Name:?list,?dtype:?bool
print('通過bool數(shù)據(jù)訪問Series結(jié)果為:\n',?series[bool])
通過bool數(shù)據(jù)訪問Series結(jié)果為:
a????0
b????1
c????2
d????3
Name:?list,?dtype:?int64
代碼清單6-8 更新Series
#?更新元素
series['a']?=?3
print('更新后的Series為:\n',?series)
更新后的Series為:
a????3
b????1
c????2
d????3
e????4
Name:?list,?dtype:?int64代碼清單6-9 追加Series和插入單個(gè)值
series1?=?pd.Series([4,?5],?index?=?['f',?'g'])
#?追加Series
print('在series插入series1后為:\n',?series.append(series1))
在series插入series1后為:
a????3
b????1
c????2
d????3
e????4
f????4
g????5
dtype:?int64#?新增單個(gè)數(shù)據(jù)
series1['h']?=?7
print('在series1插入單個(gè)數(shù)據(jù)后為:\n',?series1)在series1插入單個(gè)數(shù)據(jù)后為:
f????4
g????5
h????7
dtype:?int64代碼清單6-10 刪除Series元素
#?刪除數(shù)據(jù)
series.drop('e',?inplace?=?True)
print('刪除索引e對(duì)應(yīng)數(shù)據(jù)后的series為:\n',?series)
輸出:
刪除索引e對(duì)應(yīng)數(shù)據(jù)后的series為:
a????3
b????1
c????2
d????3
Name:?list,?dtype:?int64
class?pandas.DataFrame(data=None,?index=None,?columns=None,?dtype=None,?copy=False)
data:接收ndarray,dict,list或DataFrame。表示輸入數(shù)據(jù)。默認(rèn)為None index:接收Index,ndarray。表示索引。默認(rèn)為None columns:接收Index,ndarray。表示列標(biāo)簽(列名)。默認(rèn)為None
代碼清單6-11 通過dict創(chuàng)建DataFrame
dict1?=?{'col1':?[0,?1,?2,?3,?4],?'col2':?[5,?6,?7,?8,?9]}
print('通過dict創(chuàng)建的DataFrame為:\n',?pd.DataFrame(dict1,?index?=?['a',?'b',?'c',?'d',?'e']))
通過dict創(chuàng)建的DataFrame為:
????col1??col2
a?????0?????5
b?????1?????6
c?????2?????7
d?????3?????8
e?????4?????9代碼清單6-12 通過list創(chuàng)建DataFrame
list2?=?[[0,?5],?[1,?6],?[2,?7],?[3,?8],?[4,?9]]
print('通過list創(chuàng)建的DataFrame為:\n',
??????pd.DataFrame(list2,?index?=?['a',?'b',?'c',?'d',?'e'],?columns?=?['col1',?'col2']))
通過list創(chuàng)建的DataFrame為:
????col1??col2
a?????0?????5
b?????1?????6
c?????2?????7
d?????3?????8
e?????4?????9values:以ndarray的格式返回DataFrame對(duì)象的所有元素 index:返回DataFrame對(duì)象的Index columns:返回DataFrame對(duì)象的列標(biāo)簽 dtypes:返回DataFrame對(duì)象的數(shù)據(jù)類型 axes:返回DataFrame對(duì)象的軸標(biāo)簽 ndim:返回DataFrame對(duì)象的軸尺寸數(shù) size:返回DataFrame對(duì)象的個(gè)數(shù) shape:返回DataFrame對(duì)象的形狀
代碼清單6-13 訪問DataFrame的屬性
df?=?pd.DataFrame({'col1':?[0,?1,?2,?3,?4],?'col2':?[5,?6,?7,?8,?9]},
???????????????????index?=?['a',?'b',?'c',?'d',?'e'])
print('DataFrame的Index為:',?df.index)
#輸出:DataFrame的Index為:Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
print('DataFrame的列標(biāo)簽為:',?df.columns)
#輸出:DataFrame的列標(biāo)簽為:Index(['col1', 'col2'], dtype='object')
print('DataFrame的軸標(biāo)簽為:',?df.axes)
#輸出:DataFrame的軸標(biāo)簽為:?[Index(['a', 'b', 'c', 'd', 'e'], dtype='object'), Index(['col1', 'col2'], dtype='object')]
print('DataFrame的維度為:',?df.ndim)
#輸出:DataFrame的維度為:2
print('DataFrame的形狀為:',?df.shape)
#輸出:DataFrame的形狀為:?(5, 2)
代碼清單6-14 訪問DataFrame前后n行數(shù)據(jù)
print('默認(rèn)返回前5行數(shù)據(jù)為:\n',?df.head())????
輸出:
默認(rèn)返回前5行數(shù)據(jù)為:
????col1??col2
a?????0?????5
b?????1?????6
c?????2?????7
d?????3?????8
e?????4?????9????
print('返回后3行數(shù)據(jù)為:\n',?df.tail(3))
輸出:
返回后3行數(shù)據(jù)為:
????col1??col2
c?????2?????7
d?????3?????8
e?????4?????9
代碼清單6-15 更新DataFrame
#?更新列
df['col1']?=?[10,?11,?12,?13,?14]
print('更新列后的DataFrame為:\n',?df)
更新列后的DataFrame為:
????col1??col2
a????10?????5
b????11?????6
c????12?????7
d????13?????8
e????14?????9代碼清單6-16 采用賦值的方法插入列
#?插入列
df['col3']?=?[15,?16,?17,?18,?19]
print('插入列后的DataFrame為:\n',?df)
插入列后的DataFrame為:
????col1??col2??col3
a????10?????5????15
b????11?????6????16
c????12?????7????17
d????13?????8????18
e????14?????9????19DataFrame.drop(labels,?axis=0,?level=None,?inplace=False,?errors='raise')labels:接收string或array。表示刪除的行或列的標(biāo)簽。無默認(rèn)值 axis:接收0或1。表示執(zhí)行操作的軸向,其中0表示刪除行,1表示刪除列。默認(rèn)為0 levels:接收int或者索引名。表示索引級(jí)別。默認(rèn)為None inplace:接收bool。表示操作是否對(duì)原數(shù)據(jù)生效。默認(rèn)為False
代碼清單6-17 使用drop方法刪除數(shù)據(jù)
#?刪除列
df.drop(['col3'],?axis?=?1,?inplace?=?True)
print('刪除col3列后的DataFrame為:\n',?df)
刪除col3列后的DataFrame為:
????col1??col2
a????10?????5
b????11?????6
c????12?????7
d????13?????8
e????14?????9
#?刪除行
df.drop('a',?axis?=?0,?inplace?=?True)
print('刪除a行后的DataFrame為:\n',?df)
刪除a行后的DataFrame為:
????col1??col2
b????11?????6
c????12?????7
d????13?????8
e????14?????9
Index:一般的Index對(duì)象 MultiIndex:層次化Index對(duì)象 DatetimeIndex:Timestamp索引對(duì)象 PeriodIndex:Period索引對(duì)象
代碼清單6-18 訪問Series索引
print('series的Index為?:\n',?series.index)
series的Index為?:
?Index(['a',?'b',?'c',?'d'],?dtype='object')is_monotonic:當(dāng)各元素均大于前一個(gè)元素時(shí),返回True is_unique:當(dāng)Index沒有重復(fù)值時(shí),返回True
代碼清單6-19 訪問Index屬性
print('series中Index各元素是否大于前一個(gè):',?series.index.is_monotonic)
#輸出:series中Index各元素是否大于前一個(gè):True
print('series中Index各元素是否唯一:',?series.index.is_unique)
#輸出:series中Index各元素是否唯一:True
append:連接另一個(gè)Index對(duì)象,產(chǎn)生一個(gè)新的Index difference:計(jì)算兩個(gè)Index對(duì)象的差集,得到一個(gè)新的Index intersection:計(jì)算兩個(gè)Index對(duì)象的交集 union:計(jì)算兩個(gè)Index對(duì)象的并集 isin:計(jì)算一個(gè)Index是否在另一個(gè)Index,返回bool數(shù)組 delete:刪除指定Index的元素,并得到新的Index drop:刪除傳入的值,并得到新的Index insert:將元素插入到指定Index處,并得到新的Index unique:計(jì)算Index中唯一值的數(shù)組
代碼清單6-20 應(yīng)用Index對(duì)象的常用方法
index1?=?series.index
index2?=?series1.index
print('index1連接index2后結(jié)果為:\n',?index1.append(index2))
#輸出:index1連接index2后結(jié)果為:
#?Index(['a',?'b',?'c',?'d',?'f',?'g',?'h'],?dtype='object')
print('index1與index2的差集為:',?index1.difference(index2))
#輸出:index1與index2的差集為:Index(['a', 'b', 'c', 'd'], dtype='object')
print('index1與index2的交集為:',?index1.intersection(index2))
#輸出:index1與index2的交集為:Index([], dtype='object')
print('index1與index2的并集為:\n',?index1.union(index2))
#輸出:index1與index2的并集為:
#?Index(['a',?'b',?'c',?'d',?'f',?'g',?'h'],?dtype='object')
print('index1中的元素是否在index2中:',?index1.isin(index2))
#輸出:index1中的元素是否在index2中:?[False False False False]

延伸閱讀《Python3智能數(shù)據(jù)分析快速入門》
點(diǎn)擊上圖了解及購買
轉(zhuǎn)載請(qǐng)聯(lián)系微信:DoctorData


評(píng)論
圖片
表情
