栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

01-Pandas的数据结构-Series

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

01-Pandas的数据结构-Series

Series Series创建
import pandas as pd
import numpy as np
# 2.1 通过list创建
s1 = pd.Series([1,2,3,4,5])
s1
0    1
1    2
2    3
3    4
4    5
dtype: int64
type(s1)
pandas.core.series.Series
# 2.2 通过数组创建
arr1 = np.arange(1,6)
print(arr1)
[1 2 3 4 5]
s2 = pd.Series(arr1)
s2
0    1
1    2
2    3
3    4
4    5
dtype: int32
#索引长度和数据长度必须相同。
s2 = pd.Series(arr1,index=['a','b','c','d','e'])
s2
a    1
b    2
c    3
d    4
e    5
dtype: int32
s1.values
array([1, 2, 3, 4, 5], dtype=int64)
s1.index
RangeIndex(start=0, stop=5, step=1)
# 2.3 通过字典创建
dict = {'name':'李宁','age':18,'class':'三班'}
s3 = pd.Series(dict,index = ['name','age','class','sex'])   #  注意一下这里的索引,用字典创建的 Series对象
s3
name      李宁
age       18
class     三班
sex      NaN
dtype: object
三.Series的基本用法
# 3.1 isnull 和 notnull 检查缺失值
s3.isnull()  #判断是否为空  空就是True
name     False
age      False
class    False
sex       True
dtype: bool
s3.notnull() #判断是否不为空  非空True
name      True
age       True
class     True
sex      False
dtype: bool
# 3.2 通过索引获取数据
print(s3.index)
print(s3.values)
Index(['name', 'age', 'class', 'sex'], dtype='object')
['李宁' 18 '三班' nan]
# 下标
s3[0]
'李宁'
# 标签名
s3['age']
18
# 选取多个
s3[['name','age']]  # s3[[1,3]]  
name    李宁
age     18
dtype: object
# 切片
s3[1:3]
age      18
class    三班
dtype: object
s3['name':'class']   #标签切片 包含末端数据
name     李宁
age      18
class    三班
dtype: object
#布尔索引
s2[s2>3]
d    4
e    5
dtype: int32
# 3.3 索引与数据的对应关系不被运算结果影响
print(s2+2)
print(s2>2)
year
a    3
b    4
c    5
d    6
e    7
Name: temp, dtype: int32
year
a    False
b    False
c     True
d     True
e     True
Name: temp, dtype: bool
# 3.4 name属性
s2.name = 'temp'  #对象名
s2.index.name = 'year'  #对象索引名
s2
year
a    1
b    2
c    3
d    4
e    5
Name: temp, dtype: int32
s2.head(3)  #默认前5行
year
a    1
b    2
c    3
Name: temp, dtype: int32
s2.tail(2)  #尾部默认后5行
year
d    4
e    5
Name: temp, dtype: int32
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/350606.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号