pandas中的series數(shù)據(jù)類型詳解
更新時間:2019年07月06日 11:45:29 作者:xsan
這篇文章主要介紹了pandas中的series數(shù)據(jù)類型詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
本文介紹了pandas中的series數(shù)據(jù)類型詳解,分享給大家,具體如下:
import pandas as pd
import numpy as np
import names
'''
寫在前面的話:
1、series與array類型的不同之處為series有索引,而另一個沒有;series中的數(shù)據(jù)必須是一維的,而array類型不一定
2、可以把series看成一個定長的有序字典,可以通過shape,index,values等得到series的屬性
'''
# 1、series的創(chuàng)建
'''
(1)由列表或numpy數(shù)組創(chuàng)建
默認索引為0到N-1的整數(shù)型索引,如s1;
可以通過設(shè)置index參數(shù)指定索引,如s2;
通過這種方式創(chuàng)建的series,不是array的副本,即對series操作的同時也改變了原先的array數(shù)組,如s3
(2)由字典創(chuàng)建
字典的鍵名為索引,鍵值為值,如s4;
'''
n1 = np.array([1, 4, 5, 67, 7, 43, ])
s1 = pd.Series(n1)
# print(s1)
'''
1
4
5
67
7
43
dtype: int32
'''
s2 = pd.Series(n1, index=['a', 'b', 'c', 'd', 'e', 'f'])
# print(s2)
'''
a 1
b 4
c 5
d 67
e 7
f 43
dtype: int32
'''
# print(n1)
'''
[ 1 4 5 67 7 43]
'''
s1[2] = 100
s3 = s1
# print(s3)
'''
1
4
100
67
7
43
dtype: int32
'''
# print(n1)
'''
[ 1 4 100 67 7 43]
'''
dict1 = {}
for i in range(10, 15):
# names.get_last_name(),隨機生成英文名字
dict1[names.get_last_name()] = i
s4 = pd.Series(dict1)
# print(s4)
'''
Poole 10
Allen 11
Davis 12
Roland 13
Brehm 14
dtype: int64
'''
# 2、series的索引
'''
(1)通過index取值,可以通過下標(biāo)獲取,也可以通過指定索引獲取,如s6,s7
(2)通過.loc[](顯示索引)獲取,這種方式只能獲取顯示出來的索引,無法通過下標(biāo)獲取,如s7(推薦)
(3)隱式索引,使用整數(shù)作為索引值,使用.icol[],如s9(推薦)
'''
s5 = pd.Series(np.array([1, 5, 9, 7, 6, 4, 52, 8]), index=[list('abcdefgh')])
# print(s5)
'''
a 1
b 5
c 9
d 7
e 6
f 4
g 52
h 8
dtype: int32
'''
s6 = s5[2]
# print(s6)
'''
'''
s7 = s5['c']
# print(s7)
'''
c 9
dtype: int32
'''
s8 = s5.loc['c']
# print(s8)
'''
c 9
dtype: int32
'''
s9 = s5.iloc[2]
# print(s9)
'''
'''
# 3、series的切片
'''
1、series的切片和列表的用法類似,不同之處在于建議使用.loc[:]和.iloc[:],如s10和s11。當(dāng)然直接使用[:]也可以。
2、當(dāng)遇到特別長的series,我們支取出前5條或后5條數(shù)據(jù)時可以直接使用.head()或.tail()
'''
s5 = pd.Series(np.array([1, 5, 9, 7, 6, 4, 52, 8]), index=[list('abcdefgh')])
# print(s5)
'''
a 1
b 5
c 9
d 7
e 6
f 4
g 52
h 8
dtype: int32
'''
s10 = s5.loc['b':'g']
# print(s10)
'''
b 5
c 9
d 7
e 6
f 4
g 52
dtype: int32
'''
s11 = s5.iloc[1:7]
# print(s11)
'''
b 5
c 9
d 7
e 6
f 4
g 52
dtype: int32
'''
# 4、關(guān)于NaN
'''
(1)NaN是代表空值, 但不等于None。兩者的數(shù)據(jù)類型不一樣,None的類型為<class 'NoneType'>,而NaN的類型為<class 'float'>;
(2)可以使用pd.isnull(),pd.notnull(),或自帶isnull(),notnull()函數(shù)檢測缺失數(shù)據(jù)
'''
# print(type(None),type(np.nan))
'''
<class 'NoneType'> <class 'float'>
'''
s12 = pd.Series([1,2,None,np.nan],index=list('烽火雷電'))
# print(s12)
'''
烽 1.0
火 2.0
雷 NaN
電 NaN
dtype: float64
'''
# print(pd.isnull(s12))
'''
烽 False
火 False
雷 True
電 True
dtype: bool
'''
# print(pd.notnull(s12))
'''
烽 True
火 True
雷 False
電 False
dtype: bool
'''
# print(s12.notnull())
'''
烽 True
火 True
雷 False
電 False
dtype: bool
'''
# print(s12.isnull())
'''
烽 False
火 False
雷 True
電 True
dtype: bool
'''
# 取出series中不為空的值
# print(s12[s12.notnull()])
'''
烽 1.0
火 2.0
dtype: float64
'''
# series的name屬性
'''
'''
s12.name = '風(fēng)水'
# print(s12)
'''
烽 1.0
火 2.0
雷 NaN
電 NaN
Name: 風(fēng)水, dtype: float64
'''
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
Python中flatten( ),matrix.A用法說明
這篇文章主要介紹了Python中flatten( ),matrix.A用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
pytorch教程resnet.py的實現(xiàn)文件源碼分析
torchvision.models這個包中包含alexnet、densenet、inception、resnet、squeezenet、vgg等常用的網(wǎng)絡(luò)結(jié)構(gòu),并且提供了預(yù)訓(xùn)練模型,可以通過簡單調(diào)用來讀取網(wǎng)絡(luò)結(jié)構(gòu)和預(yù)訓(xùn)練模型2021-09-09
pyecharts如何實現(xiàn)顯示數(shù)據(jù)為百分比的柱狀圖
這篇文章主要介紹了pyecharts如何實現(xiàn)顯示數(shù)據(jù)為百分比的柱狀圖,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

