博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python笔记:访问或修改 Pandas Series 中的元素以及相关运算
阅读量:4249 次
发布时间:2019-05-26

本文共 4292 字,大约阅读时间需要 14 分钟。

注: 我们这一篇文章中使用的数据,参考上一篇文章

访问 Pandas Series 中的元素

  • 可以通过在方括号 [ ] 内添加索引标签或数字索引访问元素,就像访问 NumPy ndarray 中的元素一样。

  • 因为我们可以使用数字索引,因此可以使用正整数从 Series 的开头访问数据,或使用负整数从末尾访问。

  • 为了清晰地表明我们指代的是索引标签还是数字索引,Pandas Series 提供了两个属性 .loc 和 .iloc :

  • 属性 .loc 表示 位置,用于明确表明我们使用的是标签索引
  • 属性 .iloc 表示整型位置,用于明确表明我们使用的是数字索引。

使用不同方法访问 Pandas Series 中的元素举例:

import pandas as pd# 使用单一索引标签访问Pandas Series 中的数据:print('我们需要买多少鸡蛋:', groceries['eggs'])print()# we can access multiple index labels# 通过多个索引标签访问Pandas Series 中的数据:print('我们需要牛奶和面包吗:\n', groceries[['milk', 'bread']]) print()# 使用loc属性,添加多个索引标签访问Pandas Series中的数据print('我们需要买多少鸡蛋和苹果:\n', groceries.loc[['eggs', 'apples']]) print()# 使用多个数字索引访问Pandas Series中的数据print('我们需要买多少鸡蛋和苹果:\n',  groceries[[0, 1]]) print()# 使用负的数字索引访问Pandas Series中的数据print('我们需要面包吗:\n', groceries[[-1]]) print()# 使用单个数字索引访问 Pandas Series 中的数据print('我们需要买多少鸡蛋: ', groceries[0]) print()# 使用iloc属性,添加多个数字索引访问 Pandas Series 中的数据print('我们需要牛奶和面包吗 :\n', groceries.iloc[[2, 3]])

输出:

我们需要买多少鸡蛋: 30我们需要牛奶和面包吗: milk     Yesbread     Nodtype: object我们需要买多少鸡蛋和苹果: eggs      30apples     6dtype: object我们需要买多少鸡蛋和苹果: eggs      30apples     6dtype: object我们需要面包吗: bread    Nodtype: object我们需要买多少鸡蛋:  30我们需要牛奶和面包吗 : milk     Yesbread     Nodtype: object

下面我们来修改 Pandas Series 中的元素

import pandas as pd# 输出原始数据print('输出原始的生活用品列表:\n', groceries)# 把索引为eggs的数据修改为2groceries['eggs'] = 2print()print('输出修改后的生活用品列表:\n', groceries)

输出:

输出原始的生活用品列表: eggs        2apples      6milk      Yesbread      Nodtype: object输出修改后的生活用品列表: eggs        2apples      6milk      Yesbread      Nodtype: object

使用 .drop() 方法删除 Pandas Series 中的条目

  • Series.drop(label) 方法会从给定 Series 中删除给定的 label。
  • Series.drop(label) 方法不在原地从 Series 中删除元素,即不会更改被修改的原始 Series。
# 输出原始的生活用品列表print('原始的生活用品列表:\n', groceries)print()# We remove apples from our grocery list. The drop function removes elements out of place# 使用drop从生活用品中删除applesprint('删除apples之后:\n', groceries.drop('apples'))print()# 再次输出 原生活用品列表,发现还有apples的数据print('再次输出原生活用品列表:\n', groceries)

原地修改Pandas Series中的数据

  • 通过在 .drop() 方法中将关键字 inplace 设为 True,原地地从 Pandas Series 中删除条目
# 输出原始的生活用品列表print('输出原始的生活用品列表:\n', groceries)# 设置关键词inplace为True, 原地删除该条目groceries.drop('apples', inplace = True)print()# 再次输出原始生活用品列表,发现已被删除print('再次输出原始生活用品列表:\n', groceries)

对 Pandas Series 执行元素级算术运算

# 创建一个只存储水果的生活用品列表的Pandas Seriesfruits= pd.Series(data = [10, 6, 3,], index = ['apples', 'oranges', 'bananas'])# We display the fruits Pandas Seriesprint('输出原始水果列表: \n' , fruits);print()print('加法运算 => fruits + 2:\n', fruits + 2)print()print('减法运算 => fruits - 2:\n', fruits - 2)print()print('乘法运算 => fruits * 2:\n', fruits * 2)print()print('除法运算 => fruits / 2:\n', fruits / 2)print()

输出:

输出原始水果列表:  apples     10oranges     6bananas     3dtype: int64加法运算 => fruits + 2: apples     12oranges     8bananas     5dtype: int64减法运算 => fruits - 2: apples     8oranges    4bananas    1dtype: int64乘法运算 => fruits * 2: apples     20oranges    12bananas     6dtype: int64除法运算 => fruits / 2: apples     5.0oranges    3.0bananas    1.5dtype: float64

对 Pandas Series 中的所有元素应用 NumPy 中的数学函数

import pandas as pdimport numpy as npfruits= pd.Series(data = [10, 6, 3,], index = ['apples', 'oranges', 'bananas'])print('输出原始fruits :\n', fruits)print()# 对fruits进行以e为底的指数运算:print('EXP(X) = \n', np.exp(fruits))print() # 对fruits进行开平方根运算:print('SQRT(X) =\n', np.sqrt(fruits))print()# 对fruits进行二次幂运算print('POW(X,2) =\n',np.power(fruits,2))

对 Pandas Series 中的部分条目应用算术运算

import pandas as pdimport numpy as npfruits= pd.Series(data = [10, 6, 3,], index = ['apples', 'oranges', 'bananas'])print('输出原始 fruits:\n ', fruits)print()# 为bananas条目的数据+2print('香蕉数据 + 2 = ', fruits['bananas'] + 2)print()# 为apple条目的数据-2print('苹果数据 - 2 = ', fruits.iloc[0] - 2)print()# 对apples和oranges的条目的数据乘上2print('苹果和橘子的数据 * 2:\n', fruits[['apples', 'oranges']] * 2)print()# 苹果和橘子的数据 / 2print('苹果和橘子的数据 / 2:\n', fruits.loc[['apples', 'oranges']] / 2)

输出:

输出原始 fruits:  apples     10oranges     6bananas     3dtype: int64香蕉数据 + 2 =  5苹果数据 - 2 =  8苹果和橘子的数据 * 2: apples     20oranges    12dtype: int64苹果和橘子的数据 / 2: apples     5.0oranges    3.0dtype: float64

对具有混合数据类型的 Pandas Series 应用算术运算

  • 前提是该算术运算适合 Series 中的所有数据类型,否则会出错。
fruits * 2print('.........')groceries * 2

输出:

apples     20oranges    12bananas     6dtype: int64.........eggs          4milk     YesYesbread      NoNodtype: object

转载地址:http://qzwei.baihongyu.com/

你可能感兴趣的文章
C语言 寻找数据中的众数
查看>>
设置cmd命令窗口的起始位置
查看>>
写给四年前刚开始编程的自己
查看>>
谷歌面试题,你敢回答吗。
查看>>
Android 安全攻防(一):SEAndroid的编译
查看>>
Android 安全攻防(二): SEAndroid bionic
查看>>
cuda如何做软连接切换
查看>>
小飞机的一些设置解释
查看>>
目标检测里正负样本和数据增广
查看>>
python和C++的交互方式
查看>>
ubuntu快捷键
查看>>
npaint (31M)-图片去水印等
查看>>
学英语以及中文快速阅读的启迪,从“为什么全世界只有中日两个国家弹幕视频网站成为流行?”说开去
查看>>
什么是人工神经网络
查看>>
神经网络的发展历史
查看>>
TED演讲:Jeff Hawkins.大脑的工作原理是什么
查看>>
所谓的语义信息
查看>>
Predictive learning vs. representation learning
查看>>
android SDK工具下载
查看>>
Hibernateday02表的唯一外键
查看>>