02.用于数据科学的 Python 基础知识之pandas(上)

Series

Series 是带标签的一维数组,可存储整数、浮点数、字符串、Python 对象等类型的数据。轴标签统称为索引,它由两部分组成

  • values:一组数据(ndarray类型)
  • index:相关的数据索引标签

形式如下:

02.用于数据科学的 Python 基础知识之pandas(上)

特点:标签与数据默认对齐,除非特殊情况,一般不会断开连接,因此通过索引取值非常方便,不需要循环,可以直接通过字典方式,key 获取value

创建Series

通过列表创建Series

不指定索引(隐式索引):

list_values = [1, 2, 3, 5, 25, 17]
pd.Series(list_values) # 可以通过index指定索引,如果不指定索引,则会自动从0开始生成索引,我们叫做隐式索引

输出结果为:

0     1
1     2
2     3
3     5
4    25
5    17
dtype: int64

指定索引(显式索引):

list_values = [1, 2, 3, 5, 25, 17]
labels = ['A', 'B', 'C', 'D', 'E', 'F']
pd.Series(list_values, index=labels)

输出结果为:

A     1
B     2
C     3
D     5
E    25
F    17
dtype: int64

通过numpy创建Series

pd.Series(np.random.randint(1,10,size=(3,)),index=['a','b','c'])

输出结果为:

a    7
b    8
c    6
dtype: int64

通过字典创建Series

dic = {"A":1,"B":2,"C":3,"D":2}
pd.Series(dic)

输出结果为:

A    1
B    2
C    3
D    2
dtype: int64

原创文章,作者:朋远方,如若转载,请注明出处:https://caovan.com/02-yongyushujukexuede-python-jichuzhishizhipandas/.html

(0)
打赏 微信扫一扫 微信扫一扫
朋远方的头像朋远方
上一篇 2022年11月10日 下午5:03
下一篇 2022年11月12日 下午12:49

相关推荐

发表回复

登录后才能评论