Pandas基础2.1|Python学习笔记

import numpy as np
import pandas as pd
df = pd.read_csv('./data/table.csv',index_col='ID')
df
Unnamed: 0SchoolClassGenderAddressHeightWeightMathPhysics
ID
11010S_1C_1Mstreet_11736334.0A+
11021S_1C_1Fstreet_21927332.5B+
11032S_1C_1Mstreet_21868287.2B+
11043S_1C_1Fstreet_21678180.4B-
11054S_1C_1Fstreet_41596484.8B+
12015S_1C_2Mstreet_51886897.0A-
12026S_1C_2Fstreet_41769463.5B-
12037S_1C_2Mstreet_61605358.8A+
12048S_1C_2Fstreet_51626333.8B
12059S_1C_2Fstreet_61676368.4B-
130110S_1C_3Mstreet_41616831.5B+

一、单级索引

  • 最常用的三类:iloc - 位置索引;loc - 标签索引;[]

loc(RMK:loc中使用的切片全部包含右端点)

单行索引:

df.loc[1103]
Unnamed: 0           2
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[[1103,1104]]
Unnamed: 0SchoolClassGenderAddressHeightWeightMathPhysics
ID
11032S_1C_1Mstreet_21868287.2B+
11043S_1C_1Fstreet_21678180.4B-
df.loc[2402:].head(5)#1304往后的所有
Unnamed: 0SchoolClassGenderAddressHeightWeightMathPhysics
ID
240231S_2C_4Mstreet_71668248.7B
240332S_2C_4Fstreet_61586059.7B+
240433S_2C_4Fstreet_21608467.7B
240534S_2C_4Fstreet_61935447.6B
df.loc[2402:2304:-1].head(5) #从2402开始从后往前取;loc取到端点
Unnamed: 0SchoolClassGenderAddressHeightWeightMathPhysics
ID
240231S_2C_4Mstreet_71668248.7B
240130S_2C_4Fstreet_21926245.3A
230529S_2C_3Mstreet_41877348.9B
230428S_2C_3Fstreet_61648195.5A-
  • 注:所有在loc中使用的切片全部包含右断电。
    作为pandas的使用者,不会关注最后一个标签再往后一位。若为左闭右开,则需要先知道再后面一列的名字,不便于操作。

单列索引:

df.loc[:,'Height'].head()
ID
1101    173
1102    192
1103    186
1104    167
1105    159
Name: Height, dtype: int64

多列索引:

df.loc[1201:2405,['Math','Physics']].head(5)
MathPhysics
ID
120197.0A-
120263.5B-
120358.8A+
120433.8B
120568.4B-
df.loc[:,'Gender':'Weight'].head()
GenderAddressHeightWeight
ID
1101Mstreet_117363
1102Fstreet_219273
1103Mstreet_218682
1104Fstreet_216781
1105Fstreet_415964

联合索引:

df.loc[1101:2405:4,'Address':'Math'].head()
AddressHeightWeightMath
ID
1101street_11736334.0
1105street_41596484.8
1204street_51626333.8
1303street_71888249.7
2102street_61616150.6

函数列索引:

  • lambda:匿名函数

g = lambda x: x+1

def g(x): return x+1

两者等价 --> lambda简化了函数定义的书写形式

df.loc[lambda x:x['Height'] >170 ].head()
Unnamed: 0SchoolClassGenderAddressHeightWeightMathPhysics
ID
11010S_1C_1Mstreet_11736334.0A+
11021S_1C_1Fstreet_21927332.5B+
11032S_1C_1Mstreet_21868287.2B+
12015S_1C_2Mstreet_51886897.0A-
12026S_1C_2Fstreet_41769463.5B-

loc可传入函数,且函数的输入值是整张表,输出为标量、切片、合法列表(元素出现在索引中)、合法索引

def f(x):
    return [1101,1202]
df.loc[f].head()
Unnamed: 0SchoolClassGenderAddressHeightWeightMathPhysics
ID
11010S_1C_1Mstreet_11736334.0A+
12026S_1C_2Fstreet_41769463.5B-

布尔索引:

df_1 = df['Gender'].isin(['M'])
df_1.head()
ID
1101     True
1102    False
1103     True
1104    False
1105    False
Name: Gender, dtype: bool
df.loc[df_1].head()
Unnamed: 0SchoolClassGenderAddressHeightWeightMathPhysics
ID
11010S_1C_1Mstreet_11736334.0A+
11032S_1C_1Mstreet_21868287.2B+
12015S_1C_2Mstreet_51886897.0A-
12037S_1C_2Mstreet_61605358.8A+
130110S_1C_3Mstreet_41616831.5B+
df_2 = [True if i[-1]=='4' or i[-1]=='7' else False for i in df['Address'].values]
#df_2为list
df_2
[False,
 False,
 False,
 False,
 True,
  ...]
df.loc[df_2].head()
Unnamed: 0SchoolClassGenderAddressHeightWeightMathPhysics
ID
11054S_1C_1Fstreet_41596484.8B+
12026S_1C_2Fstreet_41769463.5B-
130110S_1C_3Mstreet_41616831.5B+
130312S_1C_3Mstreet_71888249.7B
210115S_2C_1Mstreet_71748483.3C

只有布尔列表和索引子集构成的列表可传入loc

iloc方法(切片右端点不包含)

单行索引:

df.iloc[-1]
Unnamed: 0          34
School             S_2
Class              C_4
Gender               F
Address       street_6
Height             193
Weight              54
Math              47.6
Physics              B
Name: 2405, dtype: object

多行索引:

df.iloc[0:10:2]
Unnamed: 0SchoolClassGenderAddressHeightWeightMathPhysics
ID
11010S_1C_1Mstreet_11736334.0A+
11032S_1C_1Mstreet_21868287.2B+
11054S_1C_1Fstreet_41596484.8B+
12026S_1C_2Fstreet_41769463.5B-
12048S_1C_2Fstreet_51626333.8B

单列索引:

df.iloc[:,-1].head()
ID
1101    A+
1102    B+
1103    B+
1104    B-
1105    B+
Name: Physics, dtype: object

多列索引:

df.iloc[:,-1::-2].head()
PhysicsWeightAddressClassUnnamed: 0
ID
1101A+63street_1C_10
1102B+73street_2C_11
1103B+82street_2C_12
1104B-81street_2C_13
1105B+64street_4C_14

混合索引:

df.iloc[3::4,-1::-3].head()
PhysicsHeightClass
ID
1104B-167C_1
1203A+160C_2
1302A-175C_3
2101C174C_1
2105A170C_1

函数式索引:

df.iloc[lambda x:[-3],-1::-2].head()
PhysicsWeightAddressClassUnnamed: 0
ID
2403B+60street_6C_432

iloc中接受的参数智能为整数或整数列表或布尔列表,不能使用布尔Series,若要用则需要将values拿出来

df_3 = (df['Address']=='street_2').values
df_3
array([False,  True,  True,  True, False, False, False, False, False,
       False, False, False, False,  True, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False,  True, False, False,  True, False])
df.iloc[df_3].head()
Unnamed: 0SchoolClassGenderAddressHeightWeightMathPhysics
ID
11021S_1C_1Fstreet_21927332.5B+
11032S_1C_1Mstreet_21868287.2B+
11043S_1C_1Fstreet_21678180.4B-
130413S_1C_3Mstreet_21957085.2A
240130S_2C_4Fstreet_21926245.3A

[]操作符

Series的[]操作

单元素索引:

#df['*']为一个Series,作为data就传入了index,若后边又传入一个index,根据自动对齐规则(以后边指定的index为准),就变成了NaN
#df['*'].tolist()或者df['*'].values;若只有df['*']无法确定是Math的索引还是值
s = pd.Series(df['Math'].values,index = df['Address'])
s['street_2']
street_2    32.5
street_2    87.2
street_2    80.4
street_2    85.2
street_2    45.3
street_2    67.7
dtype: float64
m = pd.Series(df['Math'],index=df.index)
m[2105]
34.2
m[0:4]
ID
1101    34.0
1102    32.5
1103    87.2
1104    80.4
Name: Math, dtype: float64

函数式索引:

#lambda x: x.index[16::-6]为绝对位置切片
#lambda x: 16::-6 为元素切片
m[lambda x: x.index[16::-6]]
ID
2102    50.6
1301    31.5
1105    84.8
Name: Math, dtype: float64

布尔索引:

m>80
ID
1101    False
1102    False
1103     True
1104     True
1105     True


Name: Math, dtype: bool

m[m>80]
ID
1103    87.2
1104    80.4
1105    84.8
1201    97.0
1302    87.7
1304    85.2
2101    83.3
2205    85.4
2304    95.5
Name: Math, dtype: float64

注:在Series中[]的浮点切片不是位置比较,而是值比较,故尽量不要在行索引为浮点时使用[]操作符。

s_int = pd.Series([1,2,3,4],index = [1,3,5,6])
s_float = pd.Series([1,2,3,4],index=[1.,3.,5.,6.])
s_int
1    1
3    2
5    3
6    4
dtype: int64
s_float[2:]#2作为元素
3.0    2
5.0    3
6.0    4
dtype: int64
s_int[2:]#2作为位置
5    3
6    4
dtype: int64
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值