Pandas中Series索引的创建和基本⽤法pandas 笔记002
⽬录
⼆、Series索引的创建和基本⽤法
1.Series索引的创建
三种常⽤⽅法创建Series索引
通过列表创建Series索引
通过numpy创建的数组来创建Series索引
通过字典(⽆序)创建Series索引
isnull的用法1.1 通过列表创建Series索引
索引值默认从0开始(0索引)。
import pandas as pd
import numpy as np
a1 = pd.Series([1,2,3,4,5])
a1 #⽣成的结果左边是索引(默认的索引),对应右边的元素值,最下⾯是值的数据类型
0 1
1 2
2 3
3 4
4 5
dtype: int64
type(a1)#类型
index指定索引名:
注意:指定的索引数量必须和列表元素数量⼀致,否则报错。
#index指定索引名
a2 = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])#指定的索引数量必须和列表元素数量⼀致,否则报错
a2
a 1
b 2
c 3
d 4
e 5
dtype: int64
1.2 通过numpy创建的数组来创建Series索引
b1 = pd.Series(np.arange(1,6))
b1 #⽣成的结果左边是索引(默认的索引),对应右边的元素值
0 1
1 2
2 3
3 4
4 5
dtype: int32
type(b1)#类型
指定索引:
注意:指定的索引数量必须和列表元素数量⼀致,否则报错。
#index指定索引名
b1 = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])#指定的索引数量必须和列表元素数量⼀致,否则报错b1
a 1
b 2
c 3
d 4
e 5
dtype: int64
1.3 通过字典(⽆序数据类型)创建Series索引
dict1 ={'name':'李宁','age':'12','gender':'男'}
print(dict1)#字典是⽆序的,每次输出顺序可能不⼀样
print("="*20)
c1 = pd.Series(dict1)
print(c1)
{'name': '李宁', 'age': '12', 'gender': '男'}
====================
name 李宁
age 12
gender 男
dtype: object
可以指定索引名字和顺序:
#指定字典索引
c2 = pd.Series(dict,index=['gender','name','age'])
c2
gender 男
name 李宁
age 12
dtype: object
注意:指定的索引数量可以不等于字典的键值对数量,多余的索引则返回NAN
c3 = pd.Series(dict,index=['gender','name','age','weight'])
c3
gender 男
name 李宁
age 12
weight NaN
dtype: object
注意:⼩于则返回指定的索引,未指定不返回
c4 = pd.Series(dict,index=['gender','name'])
c4
gender 男
name 李宁
dtype: object
2. Seies索引的基本⽤法
2.1 values 获取数据
d1 = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])#指定的索引数量必须和列表元素数量⼀致,否则报错d1
a 1
b 2
c 3
d 4
e 5
dtype: int64
d1.values #获取数据,返回⼀个数组
array([1, 2, 3, 4, 5], dtype=int64)
2.2 index 获取索引
d1.index #获取索引
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
2.3 下标取值
c1:
dict1 ={'name':'李宁','age':'12','gender':'男'}
c1 = pd.Series(dict1)
print(c1)
name 李宁
age 12
gender 男
dtype: object
下标取值
print(c1[0])#取⼀个值
print("="*20)
print(c1[[0,1]])#取多个值
print("="*20)
print(c1[['name','age']])#通过指定索引名字取多个值
李宁
====================
name 李宁
age 12
dtype: object
====================
name 李宁
age 12
dtype: object
2.4 切⽚
注意:下标切⽚,取左不取右。标签切⽚,左右皆取。
#切⽚
print(c1[0:2])#下标切⽚,取左不取右
print("="*20)
print(c1['name':'gender'])#通过指定的所有名字切⽚(标签切⽚),左右都包括
name 李宁
age 12
dtype: object
====================
name 李宁
age 12
gender 男
dtype: object
2.5 布尔索引
a1:
a1 = pd.Series([1,2,3,4,5])
a1 #⽣成的结果左边是索引(默认的索引),对应右边的元素值
0 1
1 2
2 3
3 4
4 5
dtype: int64
布尔索引:
print(a1[a1>3])#返回a1中⼤于3的值
3 4
4 5
dtype: int64
索引与数据的对应关系不会被运算结果影响,就是说运算不会改变原Series索引的对应关系和值。
print(a1)
print("="*20)
print(a1[a1>3])#返回a1中⼤于3的值
print("="*20)
print(a1*2)
print("="*20)#返回a1中⼤于3的值,还是不变,因为原Series索引没变
print(a1[a1>3])
0 1
1 2
2 3
3 4
4 5
dtype: int64
====================
3 4
4 5
dtype: int64
====================
0 2
1 4
2 6
3 8
4 10
dtype: int64
====================
3 4
4 5
dtype: int64
2.6 isnull和notnull检查缺失值
c2:
c2 = pd.Series(dict,index=['gender','name','age'])
c2
gender 男
name 李宁
age 12
dtype: object
isnull():⽆缺失值(⾮NAN)返回False,否则返回True
c2.isnull()#⽆缺失值(⾮NAN)返回False,否则返回True
gender False
name False
age False
dtype: bool
notnull():⽆缺失值(⾮NAN)返回True,否则返回False
gender True
name True
age True
dtype: bool
c3:
c3 = pd.Series(dict,index=['gender','name','age','weight'])
c3
print(c3.isnull())
ull())
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论