本文共 4292 字,大约阅读时间需要 14 分钟。
注: 我们这一篇文章中使用的数据,参考上一篇文章
可以通过在方括号 [ ] 内添加索引标签或数字索引访问元素,就像访问 NumPy ndarray 中的元素一样。
因为我们可以使用数字索引,因此可以使用正整数从 Series 的开头访问数据,或使用负整数从末尾访问。
为了清晰地表明我们指代的是索引标签还是数字索引,Pandas Series 提供了两个属性 .loc 和 .iloc :
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
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)
.drop()
方法中将关键字 inplace
设为 True
,原地地从 Pandas Series 中删除条目# 输出原始的生活用品列表print('输出原始的生活用品列表:\n', groceries)# 设置关键词inplace为True, 原地删除该条目groceries.drop('apples', inplace = True)print()# 再次输出原始生活用品列表,发现已被删除print('再次输出原始生活用品列表:\n', groceries)
# 创建一个只存储水果的生活用品列表的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
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))
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
fruits * 2print('.........')groceries * 2
输出:
apples 20oranges 12bananas 6dtype: int64.........eggs 4milk YesYesbread NoNodtype: object
转载地址:http://qzwei.baihongyu.com/