python实验指导书pandas答案_Python Pandas - Series

Python Pandas - Series

Series是一维标记数组,能够保存任何类型的数据(整数,字符串,浮点数,python对象等)。 轴标签统称为索引。

pandas.Series

可以使用以下构造函数创建pandas系列 -pandas.Series( data, index, dtype, copy)

构造函数的参数如下 -S.No参数和描述

1data

数据采用各种形式,如ndarray,list,常量

2index

索引值必须是唯一且可清除的,与数据长度相同。 如果没有传递索引,则默认为np.arrange(n) 。

3dtype

dtype用于数据类型。 如果为None,则将推断数据类型

4copy

复制数据。 默认为False

可以使用各种输入创建系列,例如 -Array

Dict

Scalar value or constant

创建一个空系列

可以创建的基本系列是空系列。

例子 (Example)#import the pandas library and aliasing as pd

import pandas as pd

s = pd.Series()

print s

其output如下 -Series([], dtype: float64)

从ndarray创建一个系列

如果数据是ndarray,则传递的索引必须具有相同的长度。 如果没有传递索引,那么默认索引将是range(n) ,其中n是数组长度,即[0,1,2,3 .... range(len(array))-1].

例子1 (Example 1)#import the pandas library and aliasing as pd

import pandas as pd

import numpy as np

data = np.array(['a','b','c','d'])

s = pd.Series(data)

print s

其output如下 -0 a

1 b

2 c

3 d

dtype: object

我们没有传递任何索引,因此默认情况下,它分配的索引范围从0到len(data)-1 ,即0到3。

例子2 (Example 2)#import the pandas library and aliasing as pd

import pandas as pd

import numpy as np

data = np.array(['a','b','c','d'])

s = pd.Series(data,index=[100,101,102,103])

print s

其output如下 -100 a

101 b

102 c

103 d

dtype: object

我们在这里传递了索引值。 现在我们可以在输出中看到自定义的索引值。

从dict创建一个系列

可以将dict作为输入传递,如果未指定索引,则按排序顺序获取字典键以构造索引。 如果传递了索引,则将拉出与索引中的标签对应的数据中的值。

例子1 (Example 1)#import the pandas library and aliasing as pd

import pandas as pd

import numpy as np

data = {'a' : 0., 'b' : 1., 'c' : 2.}

s = pd.Series(data)

print s

其output如下 -a 0.0

b 1.0

c 2.0

dtype: float64

Observe - 字典键用于构造索引。

例子2 (Example 2)#import the pandas library and aliasing as pd

import pandas as pd

import numpy as np

data = {'a' : 0., 'b' : 1., 'c' : 2.}

s = pd.Series(data,index=['b','c','d','a'])

print s

其output如下 -b 1.0

c 2.0

d NaN

a 0.0

dtype: float64

Observe - 索引顺序是持久的,缺少的元素用NaN(非数字)填充。

从标量创建一个系列

如果数据是标量值,则必须提供索引。 将重复该值以匹配index的长度#import the pandas library and aliasing as pd

import pandas as pd

import numpy as np

s = pd.Series(5, index=[0, 1, 2, 3])

print s

其output如下 -0 5

1 5

2 5

3 5

dtype: int64

从具有位置的系列访问数据

系列中的数据可以类似于ndarray.中的数据访问ndarray.

例子1 (Example 1)

检索第一个元素。 我们已经知道,对于数组,计数从零开始,这意味着第一个元素存储在第零个位置,依此类推。import pandas as pd

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the first element

print s[0]

其output如下 -1

例子2 (Example 2)

检索系列中的前三个元素。 如果在其前面插入:,则将提取该索引以后的所有项目。 如果使用两个参数(在它们之间):两个索引之间的项目(不包括停止索引)import pandas as pd

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the first three element

print s[:3]

其output如下 -a 1

b 2

c 3

dtype: int64

例子3 (Example 3)

检索最后三个元素。import pandas as pd

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the last three element

print s[-3:]

其output如下 -c 3

d 4

e 5

dtype: int64

使用标签(索引)检索数据

Series类似于固定大小的dict ,您可以通过索引标签获取和设置值。

例子1 (Example 1)

使用索引标签值检索单个元素。import pandas as pd

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve a single element

print s['a']

其output如下 -1

例子2 (Example 2)

使用索引标签值列表检索多个元素。import pandas as pd

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve multiple elements

print s[['a','c','d']]

其output如下 -a 1

c 3

d 4

dtype: int64

例子3 (Example 3)

如果未包含标签,则会引发异常。import pandas as pd

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve multiple elements

print s['f']

其output如下 -…

KeyError: 'f'

数据结构简介(Introduction to Data Structures)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值