在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ Pandas系列
Pandas教程
Pandas注意事項&竅門
Pandas IO工具
Pandas重建索引
Pandas稀疏數(shù)據(jù)
Pandas時間差(Timedelta)
Pandas聚合
Pandas字符串和文本數(shù)據(jù)
Pandas分類數(shù)據(jù)
Pandas索引和選擇數(shù)據(jù)
Pandas基本功能
Pandas系列
Pandas數(shù)據(jù)幀(DataFrame)
Pandas日期功能
Pandas缺失數(shù)據(jù)
Pandas與SQL比較
Pandas迭代
Pandas合并/連接
Pandas選項和自定義
Pandas級聯(lián)
Pandas可視化
Pandas數(shù)據(jù)結(jié)構(gòu)
Pandas環(huán)境安裝配置
Pandas統(tǒng)計函數(shù)
Pandas窗口函數(shù)
Pandas面板(Panel)
Pandas排序
Pandas函數(shù)應(yīng)用
Pandas快速入門
Pandas描述性統(tǒng)計
Pandas分組(GroupBy)

Pandas系列

系列(Series)是能夠保存任何類型的數(shù)據(jù)(整數(shù),字符串,浮點(diǎn)數(shù),Python對象等)的一維標(biāo)記數(shù)組。軸標(biāo)簽統(tǒng)稱為索引。

pandas.Series

Pandas系列可以使用以下構(gòu)造函數(shù)創(chuàng)建 -

pandas.Series( data, index, dtype, copy)。

構(gòu)造函數(shù)的參數(shù)如下 -

編號 參數(shù) 描述
1 data 數(shù)據(jù)采取各種形式,如:ndarray,listconstants
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)建一個系列,如 -

  • 數(shù)組
  • 字典
  • 標(biāo)量值或常數(shù)

創(chuàng)建一個空的系列

創(chuàng)建一個基本系列是一個空系列。

示例

#import the pandas library and aliasing as pd
import pandas as pd
s = pd.Series()
print s

執(zhí)行上面示例代碼,輸出結(jié)果如下 -

Series([], dtype: float64)

從ndarray創(chuàng)建一個系列

如果數(shù)據(jù)是ndarray,則傳遞的索引必須具有相同的長度。 如果沒有傳遞索引值,那么默認(rèn)的索引將是范圍(n),其中n是數(shù)組長度,即[0,1,2,3…. range(len(array))-1] - 1]

示例1

#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)
print s

執(zhí)行上面示例代碼,輸出結(jié)果如下 -

0   a
1   b
2   c
3   d
dtype: object

這里沒有傳遞任何索引,因此默認(rèn)情況下,它分配了從0len(data)-1的索引,即:03

示例2

#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

執(zhí)行上面示例代碼,輸出結(jié)果如下 -

100  a
101  b
102  c
103  d
dtype: object

在這里傳遞了索引值?,F(xiàn)在可以在輸出中看到自定義的索引值。

從字典創(chuàng)建一個系列

字典(dict)可以作為輸入傳遞,如果沒有指定索引,則按排序順序取得字典鍵以構(gòu)造索引。 如果傳遞了索引,索引中與標(biāo)簽對應(yīng)的數(shù)據(jù)中的值將被拉出。

示例2

#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)
print s

執(zhí)行上面示例代碼,輸出結(jié)果如下 -

a 0.0
b 1.0
c 2.0
dtype: float64

注意 - 字典鍵用于構(gòu)建索引。

示例

#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

執(zhí)行上面示例代碼,輸出結(jié)果如下 -

b 1.0
c 2.0
d NaN
a 0.0
dtype: float64

注意觀察 - 索引順序保持不變,缺少的元素使用NaN(不是數(shù)字)填充。

從標(biāo)量創(chuàng)建一個系列

如果數(shù)據(jù)是標(biāo)量值,則必須提供索引。將重復(fù)該值以匹配索引的長度。

#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

執(zhí)行上面示例代碼,得到以下結(jié)果 -

0  5
1  5
2  5
3  5
dtype: int64

從具有位置的系列中訪問數(shù)據(jù)

系列中的數(shù)據(jù)可以使用類似于訪問ndarray中的數(shù)據(jù)來訪問。

示例-1

檢索第一個元素。比如已經(jīng)知道數(shù)組從零開始計數(shù),第一個元素存儲在零位置等等。

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the first element
print s[0]

執(zhí)行上面示例,得到以下結(jié)果 -

1

示例-2

檢索系列中的前三個元素。 如果a:被插入到其前面,則將從該索引向前的所有項目被提取。 如果使用兩個參數(shù)(使用它們之間),兩個索引之間的項目(不包括停止索引)。

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the first three element
print s[:3]

執(zhí)行上面示例,得到以下結(jié)果 -

a  1
b  2
c  3
dtype: int64

示例-3

檢索最后三個元素,參考以下示例代碼 -

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the last three element
print s[-3:]

執(zhí)行上面示例代碼,得到以下結(jié)果 -

c  3
d  4
e  5
dtype: int64

使用標(biāo)簽檢索數(shù)據(jù)(索引)

一個系列就像一個固定大小的字典,可以通過索引標(biāo)簽獲取和設(shè)置值。

示例1

使用索引標(biāo)簽值檢索單個元素。

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve a single element
print s['a']

執(zhí)行上面示例代碼,得到以下結(jié)果 -

1

示例2

使用索引標(biāo)簽值列表檢索多個元素。

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve multiple elements
print s[['a','c','d']]

執(zhí)行上面示例代碼,得到以下結(jié)果 -

a  1
c  3
d  4
dtype: int64

示例3

如果不包含標(biāo)簽,則會出現(xiàn)異常。

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve multiple elements
print s['f']

執(zhí)行上面示例代碼,得到以下結(jié)果 -

…
KeyError: 'f'