PANDAS第二次打卡

第2章 索引
import numpy as np
import pandas as pd
df = pd.read_csv(‘data/table.csv’,index_col=‘ID’)
df.head()
School Class Gender Address Height Weight Math Physics
ID
1101 S_1 C_1 M street_1 173 63 34.0 A+
1102 S_1 C_1 F street_2 192 73 32.5 B+
1103 S_1 C_1 M street_2 186 82 87.2 B+
1104 S_1 C_1 F street_2 167 81 80.4 B-
1105 S_1 C_1 F street_4 159 64 84.8 B+
一、单级索引

  1. loc方法、iloc方法、[]操作符
    最常用的索引方法可能就是这三类,其中iloc表示位置索引,loc表示标签索引,[]也具有很大的便利性,各有特点
    (a)loc方法(注意:所有在loc中使用的切片全部包含右端点!)
    ① 单行索引:
    df.loc[1103]
    School S_1
    Class C_1
    Gender M
    Address street_2
    Height 186
    Weight 82
    Math 87.2
    Physics B+
    Name: 1103, dtype: object
    ② 多行索引:
    df.loc[[1102,2304]]
    School Class Gender Address Height Weight Math Physics
    ID
    1102 S_1 C_1 F street_2 192 73 32.5 B+
    2304 S_2 C_3 F street_6 164 81 95.5 A-
    df.loc[1304:].head()
    School Class Gender Address Height Weight Math Physics
    ID
    1304 S_1 C_3 M street_2 195 70 85.2 A
    1305 S_1 C_3 F street_5 187 69 61.7 B-
    2101 S_2 C_1 M street_7 174 84 83.3 C
    2102 S_2 C_1 F street_6 161 61 50.6 B+
    2103 S_2 C_1 M street_4 157 61 52.5 B-
    df.loc[2402::-1].head()
    School Class Gender Address Height Weight Math Physics
    ID
    2402 S_2 C_4 M street_7 166 82 48.7 B
    2401 S_2 C_4 F street_2 192 62 45.3 A
    2305 S_2 C_3 M street_4 187 73 48.9 B
    2304 S_2 C_3 F street_6 164 81 95.5 A-
    2303 S_2 C_3 F street_7 190 99 65.9 C
    ③ 单列索引:
    df.loc[:,‘Height’].head()
    ID
    1101 173
    1102 192
    1103 186
    1104 167
    1105 159
    Name: Height, dtype: int64
    ④ 多列索引:
    df.loc[:,[‘Height’,‘Math’]].head()
    Height Math
    ID
    1101 173 34.0
    1102 192 32.5
    1103 186 87.2
    1104 167 80.4
    1105 159 84.8
    df.loc[:,‘Height’:‘Math’].head()
    Height Weight Math
    ID
    1101 173 63 34.0
    1102 192 73 32.5
    1103 186 82 87.2
    1104 167 81 80.4
    1105 159 64 84.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值