pandasiloc與loc的用法與區(qū)別
iloc根據(jù)位置信息取索引僧凰,所以其參數(shù)必為int
loc根據(jù)索引名取索引
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.rand(25).reshape([5, 5]), index=['A','B','C','D','E'], columns=['c1', 'c2', 'c3', 'c4', 'c5'])
df2 = pd.DataFrame(np.random.rand(25).reshape([5, 5]), index=[1,2,3,4,5], columns=['c1', 'c2', 'c3', 'c4', 'c5'])
# print(df1)
# print(df1.index)
#iloc 根據(jù)位置排名取索引
res1 = df1.iloc[:3,:3]
print(res1)
# loc根據(jù)索引名、列名早像,取值
res2 = df1.loc[['A','B','C'],['c1', 'c2', 'c3']]
print(res2)
#iloc 根據(jù)位置排名取索引
res3 = df2.iloc[:3,:3]
print(res3)
# loc根據(jù)索引名是钥,列名
res4 = df2.loc[[1,2,3],['c1', 'c2', 'c3']]
print(res4)
#res1掠归、res2、res3悄泥、res4結(jié)果相同