网站首页 > 基础教程 正文
from pandas import Series, DataFrame
import pandas as pd
要使用pandas,首先就得熟悉它的两个主要数据结构:Series和DateFrame。虽然它们并不能解决所有问题,但它们为大多数应用提供了一种可靠的、易于使用的基础。
Series是一种类似于一位数组的对象,它由一组数据(各种NumPy数据类型)以及一组与之相关的数据标签(即索引)组成。仅有一组数据即可产生最简单的Series:
In [26]:
obj = Series([4,7,5,-3])
obj
Out[26]:
0 4
1 7
2 5
3 -3
dtype: int64
Series的字符表现形式为:索引在左边,值在右边。由于我们没有为数据指定索引,于是会自动创建一个0到N-1(N为数据的长度)的整数型索引。你可以通过Series的values和index属性获取其数组表示形式的索引对象:
In [5]:
obj.index
Out[5]:
RangeIndex(start=0, stop=4, step=1)
In [6]:
obj.values
Out[6]:
array([ 4, 7, 5, -3])
通常,我们希望所创建的Series带有一个可以对各个数据点进行标记的索引:
In [2]:
obj2 = Series([4,7,-5,3], index=['d','b','a','c'])
obj2
Out[2]:
d 4
b 7
a -5
c 3
dtype: int64
In [3]:
obj2.index
Out[3]:
Index(['d', 'b', 'a', 'c'], dtype='object')
与普通的NumPy数据相比,你可以通过索引的方式选取Series中的单个或一组值:
In [4]:
obj2['a']
Out[4]:
-5
In [5]:
obj2['d'] = 6
obj2
Out[5]:
d 6
b 7
a -5
c 3
dtype: int64
In [6]:
obj2[['a','b','c','d']]
Out[6]:
a -5
b 7
c 3
d 6
dtype: int64
NumPy数组运算(如根据布尔型数组进行过滤、标量乘法、应用数学函数等)都会保留索引和值之间的链接
In [7]:
obj2
Out[7]:
d 6
b 7
a -5
c 3
dtype: int64
In [8]:
obj2[obj2 > 0]
Out[8]:
d 6
b 7
c 3
dtype: int64
In [9]:
obj2 * 2
Out[9]:
d 12
b 14
a -10
c 6
dtype: int64
In [11]:
import numpy as np
np.exp(obj2)
Out[11]:
d 403.428793
b 1096.633158
a 0.006738
c 20.085537
dtype: float64
还可以将Series看成是一个定长的有序字典,因为它是索引值到数据值的一个映射。它可以用在许多原本需要字典参数的函数中:
In [12]:
'b' in obj2
Out[12]:
True
In [13]:
'e' in obj2
Out[13]:
False
如果数据被存放在一个Python字典中,也可以直接通过这个字典来创建Series:
In [14]:
sdata = {'Ohio':35000,'Texas':71000,'Oregon':16000,'Utah':5000}
In [15]:
obj3 = Series(sdata)
obj3
Out[15]:
Ohio 35000
Texas 71000
Oregon 16000
Utah 5000
dtype: int64
如果只传入一个字典,则结果Series中的索引就是原字典的键(有序排列)。
In [32]:
states = ['California','Ohio','Oregon','Texas']
In [33]:
obj4 = Series(sdata, index=states)
In [34]:
obj4
Out[34]:
California NaN
Ohio 35000.0
Oregon 16000.0
Texas 71000.0
dtype: float64
在上面的例子中,sdata中跟states索引相匹配的那3个值会被找出来并放到相应的位置上,但由于'California'所对应的sdata值找不到,所以其结果就为NaN(即'非数字',在pandas中,它用于表示缺失或NA值)。我将使用缺失或NA表示缺失数据。pandas的isnull和notnull函数可用于检测缺失数据:
In [35]:
pd.isnull(obj4)
Out[35]:
California True
Ohio False
Oregon False
Texas False
dtype: bool
In [36]:
pd.notnull(obj4)
Out[36]:
California False
Ohio True
Oregon True
Texas True
dtype: bool
Series也有类似的实例方法:
In [37]:
obj4.isnull()
Out[37]:
California True
Ohio False
Oregon False
Texas False
dtype: bool
在后面,我也会涉及到如何处理缺失数据的详细内容。
对于许多应用而言,Series最重要的一个功能是:它在算术运算中会自动对齐不同索引的数据:
In [38]:
obj3 + obj4
Out[38]:
California NaN
Ohio 70000.0
Oregon 32000.0
Texas 142000.0
Utah NaN
dtype: float64
数据对齐功能,后面会单独介绍。
Series对象本身及其索引都有一个name属性,该属性跟pandas其他的关键功能关系非常密切:
In [39]:
obj4.name = 'population'
obj4.index.name = 'state'
obj4
Out[39]:
state
California NaN
Ohio 35000.0
Oregon 16000.0
Texas 71000.0
Name: population, dtype: float64
Series的索引可以通过赋值的方式就地修改:
In [40]:
obj.index = ['Bob','Steve','Jeff','Ryan']
In [41]:
obj
Out[41]:
Bob 4
Steve 7
Jeff 5
Ryan -3
dtype: int64
猜你喜欢
- 2024-10-12 一文掌握Numpy矩阵 numpy矩阵乘法实现原理
- 2024-10-12 矩阵运算库:Numpy 矩阵运算库不支持点乘
- 2024-10-12 Python 的整数与 Numpy 的数据溢出
- 2024-10-12 整理20个Pandas统计函数 pandas函数汇总
- 2024-10-12 一文搞定Pandas核心概念之Series pandas的两大核心
- 2024-10-12 人工智能深度学习基础——Numpy模块知识汇总
- 2024-10-12 关于类型为numpy,TensorFlow.tensor,torch.tensor的shape变化
- 2024-10-12 Numpy基础用法汇总 numpy基础及取值操作
- 2024-10-12 想学好Python数据分析,一定要掌握的重要模块之numpy
- 2024-10-12 numpy基础之ndarray的数据类型dtype
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)