注: 我们这一篇文章中使用的数据,参考上一篇文章
访问 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 Yes
bread No
dtype: object
我们需要买多少鸡蛋和苹果:
eggs 30
apples 6
dtype: object
我们需要买多少鸡蛋和苹果:
eggs 30
apples 6
dtype: object
我们需要面包吗:
bread No
dtype: object
我们需要买多少鸡蛋: 30
我们需要牛奶和面包吗 :
milk Yes
bread No
dtype: object
下面我们来修改 Pandas Series 中的元素
import pandas as pd
# 输出原始数据
print('输出原始的生活用品列表:\n', groceries)
# 把索引为eggs的数据修改为2
groceries['eggs'] = 2
print()
print('输出修改后的生活用品列表:\n', groceries)
输出:
输出原始的生活用品列表:
eggs 2
apples 6
milk Yes
bread No
dtype: object
输出修改后的生活用品列表:
eggs 2
apples 6
milk Yes
bread No
dtype: 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从生活用品中删除apples
print('删除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 Series
fruits= pd.Series(data = [10, 6, 3,], index = ['apples', 'oranges', 'bananas'])
# We display the fruits Pandas Series
print('输出原始水果列表: \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 10
oranges 6
bananas 3
dtype: int64
加法运算 => fruits + 2:
apples 12
oranges 8
bananas 5
dtype: int64
减法运算 => fruits - 2:
apples 8
oranges 4
bananas 1
dtype: int64
乘法运算 => fruits * 2:
apples 20
oranges 12
bananas 6
dtype: int64
除法运算 => fruits / 2:
apples 5.0
oranges 3.0
bananas 1.5
dtype: float64
对 Pandas Series 中的所有元素应用 NumPy 中的数学函数
import pandas as pd
import numpy as np
fruits= 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 pd
import numpy as np
fruits= pd.Series(data = [10, 6, 3,], index = ['apples', 'oranges', 'bananas'])
print('输出原始 fruits:\n ', fruits)
print()
# 为bananas条目的数据+2
print('香蕉数据 + 2 = ', fruits['bananas'] + 2)
print()
# 为apple条目的数据-2
print('苹果数据 - 2 = ', fruits.iloc[0] - 2)
print()
# 对apples和oranges的条目的数据乘上2
print('苹果和橘子的数据 * 2:\n', fruits[['apples', 'oranges']] * 2)
print()
# 苹果和橘子的数据 / 2
print('苹果和橘子的数据 / 2:\n', fruits.loc[['apples', 'oranges']] / 2)
输出:
输出原始 fruits:
apples 10
oranges 6
bananas 3
dtype: int64
香蕉数据 + 2 = 5
苹果数据 - 2 = 8
苹果和橘子的数据 * 2:
apples 20
oranges 12
dtype: int64
苹果和橘子的数据 / 2:
apples 5.0
oranges 3.0
dtype: float64
对具有混合数据类型的 Pandas Series 应用算术运算
- 前提是该算术运算适合 Series 中的所有数据类型,否则会出错。
fruits * 2
print('.........')
groceries * 2
输出:
apples 20
oranges 12
bananas 6
dtype: int64
.........
eggs 4
milk YesYes
bread NoNo
dtype: object