Fork me on GitHub

pandas系列之Series数据类型

Pandas 系列之Series类型数据

本文开始正式写Pandas的系列文章,就从:如何在Pandas中创建数据开始。Pandas中创建的数据包含两种类型:

  • Series类型
  • DataFrame类型

内容导图

Series类型

Series 是一维数组结构,它仅由index(索引)和value(值)构成的。

Series的索引具有唯一性,索引既可以是数字,也可以是字符,系统会自动将它们转成一个object类型(pandas中的字符类型)。

DataFrame类型

DataFrame 是将数个 Series 按列合并而成的二维数据结构,每一列单独取出来是一个 Series ;除了拥有index和value之外,还有column。下图中:

  • 索引Index:0,1,2,3…….
  • 字段属性:fruit,number
  • 值value:苹果、葡萄等;200、300等

导入库

先导入两个库:

1
2
import pandas as pd
import numpy as np

Series类型创建与操作

  • 通过可迭代类型列表、元组生成
  • 通过python字典生成
  • 通过numpy数组生成

列表生成

通过列表的方式生成Series数据

1
2
3
4
5
6
7
8
9
s1 = pd.Series([7,8,9,10])
s1

# 结果
0 7
1 8
2 9
3 10
dtype: int64
1
2
3
4
5
6
7
8
9
10
11
12
s2 = pd.Series(list(range(1,8)))
s2

# 结果
0 1
1 2
2 3
3 4
4 5
5 6
6 7
dtype: int64

元组生成

下面的方法是通过元组生成Series数据

1
2
3
4
5
6
7
8
9
10
s3 = pd.Series((7,8,9,10,11))
s3

# 结果
0 7
1 8
2 9
3 10
4 11
dtype: int64

1
2
3
4
5
6
7
8
9
10
11
12
s4 = pd.Series(tuple(range(1,8)))  #  从1到8,不包含8
s4

# 结果
0 1
1 2
2 3
3 4
4 5
5 6
6 7
dtype: int64

使用字段创建

字典的键为索引,值为Series结构对应的值

1
2
3
4
5
6
7
8
9
10
11
dic_data = {"0":"苹果", "1":"香蕉", "2":"哈密瓜","3":"橙子"}

s5 = pd.Series(dic_data)
s5

# 结果
0 苹果
1 香蕉
2 哈密瓜
3 橙子
dtype: object

使用numpy数组

1
2
3
4
5
6
7
8
9
10
11
s6 = pd.Series(np.arange(3,9))
s6

# 结果
0 3
1 4
2 5
3 6
4 7
5 8
dtype: int64

指定索引(列表)

默认的索引都是从0开始的数值,可以在创建的时候指定每个索引

1
2
3
4
5
6
7
8
9
10
11
# 默认

s1 = pd.Series([7,8,9,10])
s1

# 结果
0 7
1 8
2 9
3 10
dtype: int64
1
2
3
4
5
6
7
8
9
s7 = pd.Series([7,8,9,10], index=["A","B","C","D"])  # 指定索引值
s7

# 结果
A 7
B 8
C 9
D 10
dtype: int64

指定索引(字典形式)

字典的键作为索引值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dic_data = {"水果1":"苹果",
"水果2":"香蕉",
"水果3":"哈密瓜",
"水果4":"橙子"
}

s8 = pd.Series(dic_data)
s8

# 结果
水果1 苹果
水果2 香蕉
水果3 哈密瓜
水果4 橙子
dtype: object

查看索引值

1
2
3
4
5
6
7
8
s8

# 结果
水果1 苹果
水果2 香蕉
水果3 哈密瓜
水果4 橙子
dtype: object
1
2
3
4
s8.index   # 查看索引值

# 结果
Index(['水果1', '水果2', '水果3', '水果4'], dtype='object')

查看值

1
2
3
4
5
6
7
8
s8

# 结果
水果1 苹果
水果2 香蕉
水果3 哈密瓜
水果4 橙子
dtype: object
1
2
3
4
s8.values

# 结果
array(['苹果', '香蕉', '哈密瓜', '橙子'], dtype=object)

更改索引

1
2
3
4
5
6
7
8
9
10
11
12
13
# 1、新索引
index_new = ['one', 'two', 'three', 'four']

# 2、赋值
s8.index = index_new

s8
# 结果
one 苹果
two 香蕉
three 哈密瓜
four 橙子
dtype: object

查看是否存在空值

1
2
3
4
5
6
7
8
s7

# 结果
A 7
B 8
C 9
D 10
dtype: int64
1
2
3
4
5
6
7
8
s7.isnull()  # 没有空值

# 结果
A False
B False
C False
D False
dtype: bool
1
2
3
4
5
6
7
8
s7.notnull()

# 结果
A True
B True
C True
D True
dtype: bool

查看某个索引的值

1
2
3
4
5
6
7
s7

A 7
B 8
C 9
D 10
dtype: int64

两种方式查看:

  • 通过自定义的索引查看
  • 通过对应的数值索引查看
1
2
3
s7["A"]  #  自定义的索引值

7
1
2
3
s7[0]   # 默认的数值索引

7
1
2
3
s7["D"]

10
1
2
3
s7[3]

10

将Series转成字典

1
2
3
4
5
s_dic = s7.to_dict()  # 转成字典形式
s_dic

# 结果
{'A': 7, 'B': 8, 'C': 9, 'D': 10}
1
2
3
4
type(s_dic)   # 结果显示为字典类型

# 结果
dict

给Series索引命名

1
2
3
4
5
6
7
8
s8

# 结果
one 苹果
two 香蕉
three 哈密瓜
four 橙子
dtype: object
1
2
3
s8.index  # 原索引

Index(['one', 'two', 'three', 'four'], dtype='object')
1
2
s8.index.name = "水果"  # 索引命名
s8

结果显示为:

水果
one       苹果
two       香蕉
three    哈密瓜
four      橙子
dtype: object
1
s8.index   # 更改之后的索引
Index(['one', 'two', 'three', 'four'], dtype='object', name='水果')

修改Series数值

1
2
3
4
5
6
7
8
9
s8

# 结果为
水果
one 苹果
two 香蕉
three 哈密瓜
four 橙子
dtype: object
1
2
3
s8["three"] = "西瓜"  # 等价于s8[2] = "西瓜"

s8

更改之后的值为:

水果
one      苹果
two      香蕉
three    西瓜
four     橙子
dtype: object

Series结构转成DataFrame结构

1
2
3
4
5
6
7
8
s8

水果
one 苹果
two 香蕉
three 西瓜
four 橙子
dtype: object

在将s8转成DataFrame的过程中涉及到3个函数:

  • to_frame:转成DataFrame
  • reset_index:DataFrame类型的索引重置
  • rename:DataFrame的字段属性重置

关于DataFrame的相关内容下节详细讲解,敬请期待!

扩展阅读

在之前写过的旅游攻略文章中使用pandas的很多知识点,可供学习:

本文标题:pandas系列之Series数据类型

发布时间:2021年04月28日 - 19:04

原始链接:http://www.renpeter.cn/2021/04/28/pandas%E7%B3%BB%E5%88%97%E4%B9%8BSeries%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea