Pandas基础复习-Series

简介: Pandas(panel data & Data Analysis):Python数据分析库。Pandas是基于Numpy的,专用于数据分析的Python第三方库,最适用于处理大型结构化表格数据Pandas最初是对冲...

Pandas(panel data & Data Analysis):Python数据分析库。

Pandas是基于Numpy的,专用于数据分析的Python第三方库,最适用于处理大型结构化表格数据

Pandas最初是对冲基金公司开发出来做金融量化数据分析的Python库
Pandas借鉴了R的数据结构
Pandas基于Numpy搭建,支持Numpy中定义的大部分计算
Pandas提供了大量和其他技术交互的接口(比如IO工具 (CSV, XLSX, HDF5, …),可视化(封装pyplot),方便和其他语言技术的交互和功能扩展
Pandas底层用Cython和C做了速度优化,极大提高了执行效率

Pandas库的数据类型:

  • Series 一维
  • DataFrame 二维,Series容器,最常用
  • Panel 三维,DataFrame容器

Python的list列表,Numpy的ndarray数组和Pdandas的Series

  • list:Python自带数据类型,功能简单,操作复杂,效率低
  • ndarray(Numpy):基础数据类型,关注数据结构/运算/维度(数据间关系)
  • Series(DataFrame):扩展数据类型,关注数据实际应用,数据与索引的关系

三种数据类型的区别

  • list/Series/DataFrame的值类型可以不同,ndarray的值类型必须相同
  • 从实用性、功能强弱和和可操作性比较:list < ndarray < Series(DataFrame),实践中尽量使用Pandas数据类型。
# 导入pandas库
import pandas as pd


# 创建Series数据类型
se = pd.Series([2, 4, 6, 8, 10])

se
0     2
1     4
2     6
3     8
4    10
dtype: int64
# 创建DataFrame数据类型
da = pd.DataFrame([
    [2, 4, 6, 8, 10],
    [12, 14, 16, 18, 20]
])

da
0 1 2 3 4
0 2 4 6 8 10
1 12 14 16 18 20


Python list 列表 创建Series

#默认索引
a = pd.Series([1,2,3,4]) 
a
0    1
1    2
2    3
3    4
dtype: int64
b = pd.Series([1,2,3,4],index=['a','b','c','d']) #自定义索引
b
a    1
b    2
c    3
d    4
dtype: int64
s = pd.Series([True,1,2.3,'a','你好']) #数据类型
s
0    True
1       1
2     2.3
3       a
4      你好
dtype: object

标量值 创建Series

# 必须带index
c = pd.Series(10, index=['a', 'b', 'c'])
c
a    10
b    10
c    10
dtype: int64
c_null = pd.Series(index=['a', 'b', 'c'])
c_null
a   NaN
b   NaN
c   NaN
dtype: float64
s = pd.Series([True,1,2.3,'a','你好'], index=['a', 'b', 'c', 'd', 'e'])
s
a    True
b       1
c     2.3
d       a
e      你好
dtype: object

Python字典 创建Series

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

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

import numpy as np

n = pd.Series(np.arange(5))
n
0    0
1    1
2    2
3    3
4    4
dtype: int32
m = pd.Series(np.arange(5),index=np.arange(9,4,-1))
m
9    0
8    1
7    2
6    3
5    4
dtype: int32

其他函数 创建Series

n = pd.Series(range(10))
n
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
dtype: int32

Series类型的基本操作

index和value操作

b = pd.Series([9, 8, 7, 6, 5, 4, 3], 
              ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
b
a    9
b    8
c    7
d    6
e    5
f    4
g    3
dtype: int64
# 获得索引,输出index类型,就是pandas独有的索引类型
b.index
Index(['a', 'b', 'c', 'd', 'e', 'f', 'g'], dtype='object')
# 获得数据,输出类型为array,就是np的array数组
b.values
array([9, 8, 7, 6, 5, 4, 3], dtype=int64)

引索

# 按照引索名称取值
b['b']
8
# 按照下标取值
b[1]
8
# 直接按照key取值
b.b
8
# 按照key值引索取值
b[['c','d','a']]
c    7
d    6
a    9
dtype: int64
# 错误,两套索引并存,但不能混用
b[['c','d',0]]
c    7.0
d    6.0
0    NaN
dtype: float64

切片

# 按照下标切片
b[1:]
b    8
c    7
d    6
e    5
f    4
g    3
dtype: int64
b[: 3]
a    9
b    8
c    7
dtype: int64
# 按照key引索切片
b[: 'd']
a    9
b    8
c    7
d    6
dtype: int64
b[::2]
a    9
c    7
e    5
g    3
dtype: int64
# 从头到尾反向切片,步长为-1,即最简单的列表倒序
b[::-1]
g    3
f    4
e    5
d    6
c    7
b    8
a    9
dtype: int64

类ndarray操作

  • 索引方法相同,都有[]
  • numpy中的运算和操作可用于Series类型
  • 可以通过自定义索引的列表进行切片
  • 可以通过自动索引进行切片,如果存在自定义索引,则一同被切片
b[3] # 第3个值,结果是索引的值
6
b[:3] #0-3,结果还是Series类型
a    9
b    8
c    7
dtype: int64
b[b > b.median()] #所有大于中位数的值
a    9
b    8
c    7
dtype: int64

* 类python字典的操作 *

  • 通过自定义索引访问
  • 保留字in操作
  • 使用.get()方法
b['b']
8
'c' in b # 判断此键在不在b的索引中
True
0 in b #in 不会判断自动索引
False
b.get('f',100) #从b中提取索引f的值,如果存在就取出,不存在就用 100 代替
4

根据索引对齐操作

series + series

a = pd.Series([1,2,3],['c','d','e'])
b = pd.Series([9,8,7,6],['a','b','c','d'])
a + b #结果为两个值的并集,相加时索引对齐加值,索引不对齐的没值,加完也没值
a    NaN
b    NaN
c    8.0
d    8.0
e    NaN
dtype: float64
  • Series类型在运算中会自动对齐不同索引的数据
  • ndarray基于维度运算,series基于索引运算,更精确不易出错

Series类型的name属性

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

b = pd.Series([9,8,7,6],['a','b','c','d'])
print(b.name) # 默认没有
None
b.name = 'Series对象' # 对象命名
print(b.name)
Series对象
b.index.name = '索引列' # 索引命名
b
索引列
a    9
b    8
c    7
d    6
Name: Series对象, dtype: int64

Series类型的修改

Series对象可以随时修改并立即生效

b
索引列
a    9
b    8
c    7
d    6
Name: Series对象, dtype: int64
b['a'] = 15
b
索引列
a    15
b     8
c     7
d     6
Name: Series对象, dtype: int64
b.name = 'Series'
b
索引列
a    15
b     8
c     7
d     6
Name: Series, dtype: int64
b.name = 'new series'
b['b','c'] = 20 # b[['b','c']] = 20
b
索引列
a    15
b    20
c    20
d     6
Name: new series, dtype: int64
目录
相关文章
|
29天前
|
BI 数据处理 索引
Pandas基本操作:Series和DataFrame(Python)
Pandas基本操作:Series和DataFrame(Python)
100 1
|
4月前
|
JSON 数据挖掘 数据格式
Pandas中Series、DataFrame讲解及操作详解(超详细 附源码)
Pandas中Series、DataFrame讲解及操作详解(超详细 附源码)
112 0
|
3月前
|
索引 Python
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
33 0
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
|
3月前
|
索引 Python
Python 教程之 Pandas(10)—— 访问 series 的元素
Python 教程之 Pandas(10)—— 访问 series 的元素
44 0
Python 教程之 Pandas(10)—— 访问 series 的元素
|
3月前
|
存储 SQL 索引
Python 教程之 Pandas(9)—— 创建 Pandas Series
Python 教程之 Pandas(9)—— 创建 Pandas Series
41 0
Python 教程之 Pandas(9)—— 创建 Pandas Series
|
4月前
|
前端开发 Python
Python 教程之 Pandas(12)—— series 的二元运算
Python 教程之 Pandas(12)—— series 的二元运算
36 0
|
4月前
|
前端开发 Python
Python 教程之 Pandas(13)—— series 上的转换操作
Python 教程之 Pandas(13)—— series 上的转换操作
47 0
|
5月前
|
数据挖掘 Python
【Python】数据分析:结构化数分工具 Pandas | Series 与 DataFrame | 读取CSV文件数据
【Python】数据分析:结构化数分工具 Pandas | Series 与 DataFrame | 读取CSV文件数据
49 1
|
7天前
|
存储 数据挖掘 数据处理
Pandas数据结构详解:Series与DataFrame的奥秘
【4月更文挑战第16天】Pandas的Series和DataFrame是数据处理的核心工具。Series是一维标签化数组,支持各种数据类型,可通过索引便捷访问。DataFrame是二维表格型数据结构,适合存储和操作表格数据。两者提供丰富的统计方法和操作,如筛选、排序、分组聚合。它们之间可相互转换和交互,助力高效的数据分析。理解和掌握Series和DataFrame对于数据科学至关重要。
|
3月前
|
索引 Python
Python 教程之 Pandas(13)—— series 上的转换操作
Python 教程之 Pandas(13)—— series 上的转换操作
42 0
Python 教程之 Pandas(13)—— series 上的转换操作