关于数据分析:Pandas-系列之Series类型数据

4次阅读

共计 2586 个字符,预计需要花费 7 分钟才能阅读完成。

Pandas 系列之 Series 类型数据

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

  • Series 类型
  • DataFrame 类型

<!–MORE–>

内容导图

Series 类型

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

Series 的索引具备唯一性,索引既能够是数字,也能够是字符,零碎会主动将它们转成一个 object 类型(pandas 中的字符类型)。

DataFrame 类型

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

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

导入库

先导入两个库:

import pandas as pd
import numpy as np

Series 类型创立与操作

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

列表生成

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

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

# 后果
0     7
1     8
2     9
3    10
dtype: int64
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 数据

s3 = pd.Series((7,8,9,10,11))
s3

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

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 构造对应的值

dic_data = {"0":"苹果", "1":"香蕉", "2":"哈密瓜","3":"橙子"}

s5 = pd.Series(dic_data)
s5

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

应用 numpy 数组

s6 = pd.Series(np.arange(3,9))
s6

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

指定索引(列表)

默认的索引都是从 0 开始的数值,能够在创立的时候指定每个索引

# 默认

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

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

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

指定索引(字典模式)

字典的键作为索引值

dic_data = {"水果 1":"苹果", 
            "水果 2":"香蕉", 
            "水果 3":"哈密瓜",
            "水果 4":"橙子"
           }

s8 = pd.Series(dic_data)
s8

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

查看索引值

s8

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

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

查看值

s8

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

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

更改索引

# 1、新索引
index_new = ['one', 'two', 'three', 'four'] 

# 2、赋值
s8.index = index_new

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

查看是否存在空值

s7

# 后果
A     7
B     8
C     9
D    10
dtype: int64
s7.isnull()  # 没有空值

# 后果
A    False
B    False
C    False
D    False
dtype: bool
s7.notnull()

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

查看某个索引的值

s7

A     7
B     8
C     9
D    10
dtype: int64

两种形式查看:

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

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

7
s7["D"]

10
s7[3]

10

将 Series 转成字典

s_dic = s7.to_dict()  # 转成字典模式
s_dic

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

# 后果
dict

给 Series 索引命名

s8

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

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

结果显示为:

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

s8.index   # 更改之后的索引
Index(['one', 'two', 'three', 'four'], dtype='object', name='水果')


批改 Series 数值

s8

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

s8

更改之后的值为:

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


Series 构造转成 DataFrame 构造

s8

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

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

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

对于 DataFrame 的相干内容下节具体解说,敬请期待!

扩大浏览

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

  • 海滨城市:厦门真的不止鼓浪屿
  • 娱乐之都:长沙 31 块的臭豆腐它香吗?
  • 美食之都:成都的火锅应该很辣吧!
  • 13 朝古都:西安 - 当秦始皇遇上 biangbiang 面
  • 南方明珠:南方明珠大连等你
正文完
 0