# 速戰速決 Python - python 第三方庫（pandas）: Series基礎

#### 示例如下:

thirdLib/pandas/sample1.py

``````# pandas - Series 基礎
#   Series - 帶索引的一維表
#   DataFrame - 帶索引的二維表
#
# 注 pandas 是基於 numpy 的

import pandas as pd

# 列表轉 Series
a = ['a','b','c','d','e']
b = pd.Series(a)
print(b)
'''

0    a
1    b
2    c
3    d
4    e
'''

# 實例化 Series（指定索引列，數據列）
c = pd.Series(index=['zhao', 'qian', 'sun', 'li', 'zhou'], data=[40, 25, 22, 28, 28])
print(c)
'''

zhao    40
qian    25
sun     22
li      28
zhou    28
'''

# 實例化 Series（指定索引列和索引列的數據類型，數據列和數據列的數據類型）
c = pd.Series(index=pd.Index(['zhao', 'qian', 'sun', 'li', 'zhou'], dtype=object), data=[40, 25, 22, 28, 28], dtype=int)
print(c)
'''

zhao    40
qian    25
sun     22
li      28
zhou    28
'''

# 獲取 Series 的索引列的數據和數據列的數據
print(c.values) # [40 25 22 28 28]
print(list(c.values)) # [40, 25, 22, 28, 28]
print(c.index) # Index(['zhao', 'qian', 'sun', 'li', 'zhou'], dtype='object')
print(list(c.index)) # ['zhao', 'qian', 'sun', 'li', 'zhou']

# 獲取指定索引的數據
print(c.get('zhao')) # 40
print(c['zhao']) # 40
print(c[0]) # 40
print(c[0:3])
'''
zhao    40
qian    25
sun     22
'''
print(c[[0,3]])
'''
zhao    40
li      28
'''
print(c[c<28])
'''
qian    25
sun     22
'''

# 去重
print(c.unique()) # [40 25 22 28]

# 統計不同數據出現的次數
print(c.value_counts())
'''
28    2
40    1
25    1
22    1
'''

# 判斷數據是否包含指定的值
print(c.isin([28,40]))
'''
zhao     True
qian    False
sun     False
li       True
zhou     True
'''
# 取出包含指定值的數據
print(c[c.isin([28,40])])
'''
zhao    40
li      28
zhou    28
'''

# 判斷數據是否不包含指定的值
print(~c.isin([28,40]))
'''
zhao    False
qian     True
sun      True
li      False
zhou    False
'''
# 取出不包含指定值的數據
print(c[~c.isin([28,40])])
'''
qian    25
sun     22
'''

# 判斷數據是否包含指定範圍的值
print(c.isin(c[c<26]))
'''
zhao    False
qian     True
sun      True
li      False
zhou    False
'''
# 取出包含指定範圍值的數據
print(c[c.isin(c[c<26])])
'''
qian    25
sun     22
'''

# 對所有數據做 +10 的操作
c += 10
print(c)
'''
zhao    50
qian    35
sun     32
li      38
zhou    38
'''
``````