03 Series

作者: wshsdm | 来源:发表于2019-03-20 19:07 被阅读0次

1 Series

pandas包含的数据类型

Series-表示带标签的数组

import pandas as pd

# pandas.core.series.Series--带标签的数组

# 创建Series对象

s1=pd.Series([12,6,7,8])

type(s1)

输出:

pandas.core.series.Series

s1 内容:

0 12

1    6

2    7

3    8

2 创建Series对象,可以通过index参数指定索引

mport string

import numpy as np

# 创建Series对象,可以通过index参数指定索引

s2=pd.Series(np.arange(10),index=list(string.ascii_uppercase[:10]))

s2

输出:

A 0

B    1

C    2

D    3

E    4

F    5

G    6

H    7

I    8

J    9

dtype: int64

3 将字典对象转化为Series对象

di={'a1':8,'a2':9}

s3=pd.Series(di)

s3

输出:

a1 8

a2    9

dtype: int64

4 通过astype修改元素类型

s3=s3.astype(float)

s3

输出:

a1 8.0

a2    9.0

dtype: float64

5 以通过索引或下标对series进行操作

s3['a1']

输出:

8.0

# 切片和花式索引

s2[1:4]

输出:

B 1

C    2

D    3

dtype: int64

花式索引

s2[[1,5,7]]

输出:

B 1

F    5

H    7

dtype: int64

布尔索引

s2[s2>7]

输出:

I 8

J    9

dtype: int64

6 index和values属性

s2.index

输出:

Index(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], dtype='object')

s2.values

输出:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

相关文章

网友评论

      本文标题:03 Series

      本文链接:https://www.haomeiwen.com/subject/zrgtvqtx.html