pandas以类似字典的方式来获取某一列的值
loc是指location的意思,iloc中的i是指integer。这两者的区别如下:
loc
works on labels in the index. loc对索引中的标签起作用。iloc
works on the positions in the index (so it only takes integers) iloc在索引中的位置上作用(所以它只接受整数)。
1.loc方法
通过标签索引行的数据
2.iloc方法
通过内置位置index索引行的数据
3.代码
s = Series(np.random.randint(0,150,size=6),
index=[['a','b','c','d','e','f']])
print(s)
print("*****")
# loc方法取值
print(s.loc['a'])
print(s.loc[['a','b']])
print("*****")
# iloc方法取值
print(s.iloc[0])
print(s.iloc[5])
五行结果分别为
a 16
b 84
c 36
d 81
e 66
f 125
dtype: int32
*****
a 16
dtype: int32
a 16
b 84
dtype: int32
*****
16
125