第二次打卡笔记

导入数据

import numpy as np
import pandas as pd
df = pd.read_csv('data/table.csv',index_col='ID')
df.head()

在这里插入图片描述

一、单级索引

1、loc方法(位置索引)、iloc方法(标签索引)、[ ]操作符

(a)lcc方法
①单行索引
df.loc[1103]

在这里插入图片描述

②单列索引
df.loc[:,'Height'].head()

在这里插入图片描述

③联合索引
df.loc[1102:2401:3,'Height':'Math'].head()

在这里插入图片描述

④函数式索引:
df.loc[lambda x:x['Gender']=='M'].head()

(loc中使用的函数,传入参数就是前面的df)
在这里插入图片描述

⑤布尔索引
df.loc[df['Address'].isin(['street_7','street_4'])].head()

在这里插入图片描述
(loc中能传入的只有布尔列表和索引子集构成的列表)

(b)iloc方法(切片右端点不包含)
① 单行索引
df.iloc[3]

在这里插入图片描述

②单列索引
df.iloc[:,3].head()

在这里插入图片描述

③混合索引
df.iloc[3::4,7::-2].head()

在这里插入图片描述

④函数式索引
df.iloc[lambda x:[3]].head()

在这里插入图片描述
(iloc中接收的参数只能为整数或整数列表,不能使用布尔索引)

©[ ]操作符

(Series中的浮点[ ]进行值比较,而不是位置比较)

(1)Series的[ ]操作
①单元素索引:
s = pd.Series(df['Math'],index=df.index)
s[1101]

运行结果:34.0

② 多行索引:
s[0:4]
#使用的是绝对位置的整数切片,与元素无关,这里容易混淆

在这里插入图片描述

③ 函数式索引:
s[lambda x: x.index[16::-6]]

在这里插入图片描述

④ 布尔索引:
s[s>80]

在这里插入图片描述

(2)DataFrame的[ ]操作
① 单行索引:
df[1:2]

在这里插入图片描述

②单列索引:
df['School'].head()

在这里插入图片描述

③ 多列索引:
df[['School','Math']].head()

在这里插入图片描述

④函数式索引:
df[lambda x:['Math','Physics']].head()

在这里插入图片描述

⑤布尔索引:
df[df['Gender']=='F'].head()

在这里插入图片描述
([ ]操作常用于列选择或布尔选择,尽量避免行的选择)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值