首先,介绍这三种方法的概述:
locgets rows (or columns) with particularlabelsfrom the index. loc从索引中获取具有特定标签的行(或列)。...import pandas as pd
df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns...df1的列的名字中没有叫0,1和2的。...正确的写法应该如下:
# 也就说,不论index是什么类型的,iloc只能写位置,也就是整型数字。..., 正确写法如下:
print(df1.iloc[:,0:2])
'''
a b
0 1 2
1 4 5
2 7 8
'''
# iloc索引某些行某些列
print(df1.iloc[0:2,