二、pandas系列---pandas库的Series类型

pandas系列—pandas库的Series类型

一、Series概述

Series是由一组数据及与之相关的数据索引组成
例子:

import pandas as pd

a = pd.Series([9,8,7,6]) #自动索引
print(a)

运行结果:
0 9
1 8
2 7
3 6
dtype: int64

import pandas as pd

b = pd.Series([9,8,7,6],index=['a','b','c','d']) #自定义索引
print(b)

运行结果:
a 9
b 8
c 7
d 6
dtype: int64

二、创建Series类型的方法

Series类型可由如下类型创建:python列表、标量值、python字典、ndarray、其他函数

2.1 从标量值创建

import pandas as pd

a = pd.Series(25,index=['a','b','c','d'])
print(a)

运行结果:
a 25
b 25
c 25
d 25
dtype: int64

2.2 从字典类型创建

例子1:

import pandas as pd

b = pd.Series({'a':9,'b':8,'c':7})
print(b)

运行结果:
a 9
b 8
c 7
dtype: int64
例子2:

import pandas as pd

c = pd.Series({'a':9,'b':8,'c':7},index=['c','a','b','d'])
print(c)

运行结果:
c 7.0
a 9.0
b 8.0
d NaN
dtype: float64

2.3 从ndarray类型创建

import pandas as pd
import numpy as np

d = pd.Series(np.arange(5))
e = pd.Series(np.arange(5),index = np.arange(9,4,-1))
print(d)
print(e)

运行结果:
0 0
1 1
2 2
3 3
4 4
dtype: int32

9 0
8 1
7 2
6 3
5 4
dtype: int32

三、Series类型的基本操作

3.1 简单的操作

Series类型包括index和values两部分

import pandas as pd

g = pd.Series([9,8,7,6],['a','b','c','d'])

g
Out[16]: 
a    9
b    8
c    7
d    6

g.index
Out[18]: Index(['a', 'b', 'c', 'd'], dtype='object')

g.values
Out[19]: array([9, 8, 7, 6], dtype=int64)

g['b']
Out[20]: 8

g[1]
Out[21]: 8
dtype: int64

g[['c','d',0]]  #自定义索引和自动索引不能混合使用
Out[24]: 
c    7.0
d    6.0
0    NaN
dtype: float64

g[['c','d','a']]
Out[25]: 
c    7
d    6
a    9
dtype: int64

3.3 Series类似ndarray类型的操作

import pandas as pd

m = pd.Series([9,8,7,6],['a','b','c','d'])

m
Out[32]: 
a    9
b    8
c    7
d    6
dtype: int64

m[3]
Out[33]: 6

m[:3]
Out[34]: 
a    9
b    8
c    7
dtype: int64

m[m>m.median()]
Out[35]: 
a    9
b    8
dtype: int64

np.exp(m)
Out[36]: 
a    8103.083928
b    2980.957987
c    1096.633158
d     403.428793
dtype: float64

3.3 Series类似python字典类型的操作

import pandas as pd

h = pd.Series([9,8,7,6],['a','b','c','d'])

h['b']
Out[27]: 8

'c' in h  #保留字in只会判断自定义索引
Out[28]: True

0 in h
Out[29]: False

b.get('f',100)  #若存在,就返回‘f’对应的值,若不存在,就返回100
Out[30]: 100

3.4 Series类型的对齐操作

Series在运算中回自动对齐不同索引的数据

import pandas as pd

p = pd.Series([9,8,7,6],['a','b','c','d'])
q = pd.Series([1,2,3],['c','b','e'])

p+q
Out[39]: 
a     NaN
b    10.0
c     8.0
d     NaN
e     NaN
dtype: float64

3.5 Series类型的name属性

Series对象和索引都可以都一个名字,存储在属性.name中

import pandas as pd

s = pd.Series([9,8,7,6],['a','b','c','d'])

s
Out[42]: 
a    9
b    8
c    7
d    6
dtype: int64

s.name

s.name='Series对象'
s.index.name='索引列'

s
Out[46]: 
索引列
a    9
b    8
c    7
d    6
Name: Series对象, dtype: int64

3.6 Series类型的修改

import pandas as pd

t = pd.Series([9,8,7,6],['a','b','c','d'])

t['a']=15
t.name = 'Series'

t
Out[50]: 
a    15
b     8
c     7
d     6
Name: Series, dtype: int64

t.name='New Series'
t['b','c']=20

t
Out[53]: 
a    15
b    20
c    20
d     6
Name: New Series, dtype: int64
  • 注1:Series是一维带标签数组
  • 注2:Series基本操作类似ndarray和字典,根据索引对齐

参考资料:北京理工大学嵩天老师教学视频

猜你喜欢

转载自blog.csdn.net/skyli114/article/details/77719375