Python数据分析与展示:Series类型简单操作-8

简介: Python数据分析与展示:Series类型简单操作-8

Pandas是Python第三方库,提供高性能易用数据类型和分析工具


官网文档:http://pandas.pydata.org/pandas-docs/stable/10min.html


引入:


import pandas as pd

1

Pandas基于NumPy实现,常与NumPy和Matplotlib一同使用


两个数据类型:Series, DataFrame


基于上述数据类型的各类操作


基本操作

运算操作

特征类操作

关联类操作

image.png

Series类型

Series类型由一组数据及与之相关的数据索引组成


自动索引

自定义索引

Series是一维带“标签”数组

结构:data_a index_0

Series基本操作类似ndarray和字典,根据索引对齐


Series类型创建:

Python列表,index与列表元素个数一致

标量值,index表达Series类型的尺寸

Python字典,键值对中的“键”是索引,index从字典中进行选择操作

ndarray,索引和数据都可以通过ndarray类型创建

其他函数,range()函数等

Series类型基本操作

Series类型包括index和values两部分


.index 获得索引

.values 获得数据

Series类型的操作类似ndarray类型


索引方法相同,采用[]

NumPy中运算和操作可用于Series类型

可以通过自定义索引的列表进行切片

可以通过自动索引进行切片,如果存在自定义索引,则一同被切片

Series类型的操作类似Python字典类型:


通过自定义索引访问

保留字in操作

使用.get()方法

Series类型对齐操作

Series+ Series

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


Series类型name属性

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


Series类型的修改

对获取的值进行赋值


代码示例

# -*- coding: utf-8 -*-
# @File    : series_demo.py
# @Date    : 2018-05-19
import pandas as pd
# 创建Series对象
d = pd.Series(range(5))
print(d)
"""
0    0
1    1
2    2
3    3
4    4
dtype: int64
"""
# 计算前N项和
print(d.cumsum())
"""
0     0
1     1
2     3
3     6
4    10
dtype: int64
"""
# 自动索引
d = pd.Series([1, 2, 3, 4, 5])
print(d)
"""
0    1
1    2
2    3
3    4
4    5
dtype: int64
"""
# 自定义索引
d = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"])
print(d)
"""
a    1
b    2
c    3
d    4
e    5
dtype: int64
"""
# 从标量值创建, 不能省略index
s = pd.Series(20, index=["a", "b", "c"])
print(s)
"""
a    20
b    20
c    20
dtype: int64
"""
# 从字典类型创建
s = pd.Series({"a": 1, "b": 2, "c": 3})
print(s)
"""
a    1
b    2
c    3
dtype: int64
"""
# index从字典中进行选择操作
s = pd.Series({"a": 1, "b": 2, "c": 3}, index=["c", "a", "b", "d"])
print(s)
"""
c    3.0
a    1.0
b    2.0
d    NaN
dtype: float64
"""
# 从ndarray类型创建
import numpy as np
s = pd.Series(np.arange(5))
print(s)
"""
0    0
1    1
2    2
3    3
4    4
dtype: int32
"""
# 指定索引
s = pd.Series(np.arange(5), index=np.arange(9, 4, -1))
print(s)
"""
9    0
8    1
7    2
6    3
5    4
dtype: int32
"""
# Series基本操作
s = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"])
# 获得索引
print(s.index)
# Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
# 获得值
print(s.values)
# [1 2 3 4 5]
# 自动索引和自定义索引并存 但不能混
print(s[0])
# 1
print(s["a"])
# 1
# 切片操作
print(s[["a", "b"]])
"""
a    1
b    2
dtype: int64
"""
# 类似ndarray类型
print(s[:3])
"""
a    1
b    2
c    3
dtype: int64
"""
print(s[s>s.median()])
"""
d    4
e    5
dtype: int64
"""
print(np.exp(s))
"""
a      2.718282
b      7.389056
c     20.085537
d     54.598150
e    148.413159
dtype: float64
"""
# 类似Python字典类型
print("b" in s)
# True
print(s.get("g", 100))
# 100
# Series类型对齐操作
a = pd.Series([1, 2, 3], index=["a", "b", "c"])
b = pd.Series([5, 6, 7, 8], index=["a", "b", "d", "e"])
print(a+b)
"""
a    6.0
b    8.0
c    NaN
d    NaN
e    NaN
dtype: float64
"""
# Series类型name属性
s = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"])
s.name="Series"
s.index.name = "索引"
print(s)
"""
索引
a    1
b    2
c    3
d    4
e    5
Name: Series, dtype: int64
"""
# Series修改
s = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"])
s[0] = 666
print(s)
"""
0    666
1      2
2      3
3      4
4      5
dtype: int64
"""
s["a", "b"] = 20
print(s)
"""
a    20
b    20
c     3
d     4
e     5
dtype: int64
"""
# Series删除元素
s = pd.Series([1, 2, 3, 4, 5, 6], index=["a", "b", "c", "d", "e", "f"])
print(s)
"""
a    1
b    2
c    3
d    4
e    5
f    6
dtype: int64
"""
s1 = s.drop(["a", "b"])
print(s1)
"""
c    3
d    4
e    5
f    6
dtype: int64
"""
相关文章
|
5月前
|
Python
以下是一些常用的图表类型及其Python代码示例,使用Matplotlib和Seaborn库。
以下是一些常用的图表类型及其Python代码示例,使用Matplotlib和Seaborn库。
|
4月前
|
存储 索引 Python
Python散列类型(1)
【10月更文挑战第9天】
|
4月前
|
计算机视觉 Python
Python实用记录(一):如何将不同类型视频按关键帧提取并保存图片,实现图片裁剪功能
这篇文章介绍了如何使用Python和OpenCV库从不同格式的视频文件中按关键帧提取图片,并展示了图片裁剪的方法。
126 0
|
2月前
|
数据可视化 Python
以下是一些常用的图表类型及其Python代码示例,使用Matplotlib和Seaborn库。
通过这些思维导图和分析说明表,您可以更直观地理解和选择适合的数据可视化图表类型,帮助更有效地展示和分析数据。
92 8
|
3月前
|
Python
在 Python 中实现各种类型的循环判断
在 Python 中实现各种类型的循环判断
46 2
|
4月前
|
存储 数据安全/隐私保护 索引
|
4月前
|
Python
【10月更文挑战第6天】「Mac上学Python 11」基础篇5 - 字符串类型详解
本篇将详细介绍Python中的字符串类型及其常见操作,包括字符串的定义、转义字符的使用、字符串的连接与格式化、字符串的重复和切片、不可变性、编码与解码以及常用内置方法等。通过本篇学习,用户将掌握字符串的操作技巧,并能灵活处理文本数据。
68 1
【10月更文挑战第6天】「Mac上学Python 11」基础篇5 - 字符串类型详解
|
4月前
|
Python
【10月更文挑战第6天】「Mac上学Python 10」基础篇4 - 布尔类型详解
本篇将详细介绍Python中的布尔类型及其应用,包括布尔值、逻辑运算、关系运算符以及零值的概念。布尔类型是Python中的一种基本数据类型,广泛应用于条件判断和逻辑运算中,通过本篇的学习,用户将掌握如何使用布尔类型进行逻辑操作和条件判断。
76 1
【10月更文挑战第6天】「Mac上学Python 10」基础篇4 - 布尔类型详解
WK
|
4月前
|
存储 Python
Python内置类型名
Python 内置类型包括数字类型(int, float, complex)、序列类型(str, list, tuple, range)、集合类型(set, frozenset)、映射类型(dict)、布尔类型(bool)、二进制类型(bytes, bytearray, memoryview)、其他类型(NoneType, type, 函数类型等),提供了丰富的数据结构和操作,支持高效编程。
WK
33 2
|
4月前
|
存储 编译器 索引
Python 序列类型(2)
【10月更文挑战第8天】
Python 序列类型(2)