df=pd.read_csv(r"C:\Users\15755\Desktop\大学\新建文件夹\joyful-pandas-master\data\table.csv",index_col='ID')
在Series中[]的浮点切片不是进行位置比较而是值比较。
s_float = pd.Series([1,2,3,4],index=[1.,3.,5.,6.])
s_float = pd.Series([1,2,3,4],index=[1.,3.,5.,6.])
print(s_float[2:])
df.loc[1101:1105]
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+
切片是全闭。由于索引的特殊性,如果设置为右开需要知道再往后一列的名字,不方便。(iloc左闭右开)
按列索引
df.loc[:,['Gender','Math']].sort_values(by='Math').tail()
Gender Math
ID
2205 F 85.4
1103 M 87.2
1302 F 87.7
2304 F 95.5
1201 M 97.0
函数式索引
df.loc[lambda x:x['Gender']=='F'].head()
#loc中使⽤的函数传参数就是⾯的df
School Class Gender Address Height Weight Math Physics
ID
1102 S_1 C_1 F street_2 192 73 32.5 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+
1202 S_1 C_2 F street_4 176 94 63.5 B-
1204 S_1 C_2 F street_5 162 63 33.8 B
布尔索引
df.loc[df['Address'].isin(['street_1','street_2'])]
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-
1302 S_1 C_3 F street_1 175 57 87.7 A-
1304 S_1 C_3 M street_2 195 70 85.2 A
2204 S_2 C_2 M street_1 175 74 47.2 B-
2401 S_2 C_4 F street_2 192 62 45.3 A
2404 S_2 C_4 F street_2 160 84 67.7 B
cut
math_interval = pd.cut(df['Math'],bins=[0,40,60,80,100])
ID
1101 (0, 40]
1102 (0, 40]
1103 (80, 100]
1104 (80, 100]
1105 (80, 100]
Name: Math, dtype: category
Categories (4, interval[int64]): [(0, 40] < (40, 60] < (60, 80] < (80, 100]]
a.from_tuple
1.元组
tuples = [('A','a')