dataframe是一張二維表,行(索引),列(標簽)
loc通過列名值進行截取,而iloc通過索引截取
import numpy as np import pandas as pd test_array=np.arange(16).reshape(4,4)#四行四列數值到16的數組 test1=pd.DataFrame(test_array,index=['One','Two','Three',"Four"],columns=['a','b','c','d']) '''loc''' test1.loc['One']#讀取one行數據 test1.loc['One','a':'c']#讀取one行數據中a到c列 test1.loc['One':'Three','a':'c']#讀取one-Three行數據中a到c列 test1.loc[['One','Three'],'a':'c']#讀取one和Three行數據中a到c列 '''iloc''' test1.iloc[0]#讀取One行數據 test1.iloc[0,0:3]#讀取one行數據中a到c列 test1.iloc[0:3,0:3]#讀取one-Three行數據中a到c列 test1.iloc[[0,2],0:3]#讀取one和Three行數據中a到c列