pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。pandas提供了大量能使我们快速便捷地处理数据的函数和方法。你很快就会发现,它是使Python成为强大而高效的数据分析环境的重要因素之一。
import pandas as pd from pandas import Series,DataFrame import numpy as np
Series是一种类似与一维数组的对象,由下面两个部分组成:
# 有两种创建方式 # 1.由列表或numpy数组创建(默认索引为0到N-1的整数型索引) # 使用列表创建Series Series(data=[1,2,3,4,5],name=‘zzz‘) # 结果 0 1 1 2 2 3 3 4 4 5 Name: zzz, dtype: int64 # 使用numpy创建Series Series(data=np.random.randint(0,10,size=(5,))) # 结果 0 7 1 6 2 2 3 2 4 2 dtype: int32 # 还可以通过设置index参数指定索引 s = Series(data=np.random.randint(0,10,size=(5,)),index=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]) # 结果 a 2 b 5 c 6 d 6 e 3 dtype: int32 # 2.由字典创建:不能再使用index.但是依然存在默认索引 # 注意:数据源必须为一维数据 dic = { ‘语文‘:80, ‘数学‘:95 } s = Series(data=dic) # 结果 数学 95 语文 80 dtype: int64
可以使用中括号取单个索引(此时返回的是元素类型),或者中括号里一个列表取多个索引(此时返回的是一个Series类型)。
(1) 显式索引:
- 使用index中的元素作为索引值
- 使用s.loc[](推荐):注意,loc中括号中放置的一定是显示索引
注意,此时是闭区间
(2) 隐式索引:
- 使用整数作为索引值
- 使用.iloc[](推荐):iloc中的中括号中必须放置隐式索引
注意,此时是半开区间
s = Series(data=np.random.randint(0,10,size=(5,)),index=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]) # s的值 a 7 b 6 c 3 d 1 e 8 dtype: int32 # 隐式索引 s[0] # 结果 7 s[[0,1]] # 结果 a 7 b 6 dtype: int32 s.iloc[0] # 结果 7 s.iloc[[0,1]] # 结果 a 7 b 6 dtype: int32 # 显示索引 s.a # 结果 7 s[[‘a‘,‘d‘]] # 结果 a 7 d 1 dtype: int32 s.loc[‘a‘] # 结果 7 s.loc[[‘a‘,‘b‘]] # 结果 a 7 b 6 dtype: int32
原文:https://www.cnblogs.com/Zzbj/p/10386259.html