1.1 loc[1]表示索引的是第1行(index 是整數)spa
import pandas as pd data = [[1,2,3],[4,5,6]] index = [0,1] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) df.loc[1] ''' a 4 b 5 c 6 ''' df a b c 0 1 2 3 1 4 5 6
1.2 loc[‘d’]表示索引的是第’d’行(index 是字符)code
data = [[1,2,3],[4,5,6]] index = ['d','e'] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) df.loc['d'] ''' a 1 b 2 c 3 ''' df a b c d 1 2 3 e 4 5 6
1.3 loc能夠獲取多行數據blog
data = [[1,2,3],[4,5,6]] index = ['d','e'] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) df.loc['d':] ''''' a b c d 1 2 3 e 4 5 6 '''
1.4 loc擴展——索引某行某列索引
import pandas as pd data = [[1,2,3],[4,5,6]] index = ['d','e'] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) df.loc['d',['b','c']] ''''' b 2 c 3 '''
1.5 loc擴展——索引某列pandas
import pandas as pd data = [[1,2,3],[4,5,6]] index = ['d','e'] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) df.loc[:,['c']] ''' c d 3 e 6 '''
固然獲取某列數據最直接的方式是df.[列標籤],可是當列標籤未知時能夠經過這種方式獲取列數據。
須要注意的是,dataframe的索引[1:3]是包含1,2,3的。class
.iloc
則是基於序號的索引(仍是行優先),從0到length-1
。import
2.1 獲取單行擴展
import pandas as pd data = [[1,2,3],[4,5,6]] index = ['d','e'] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) df.loc[1] ''' a 4 b 5 c 6 '''
2.2 索引多行im
import pandas as pd data = [[1,2,3],[4,5,6]] index = ['d','e'] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) df.iloc[0:] """ a b c d 1 2 3 e 4 5 6 """
2.3 索引列數據數據
import pandas as pd data = [[1,2,3],[4,5,6]] index = ['d','e'] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) df.iloc[:,[1]] ''''' b d 2 e 5 '''
.ix
則至關於上述兩個之和,兩種index都能處理。
3.1 經過行號索引
import pandas as pd data = [[1,2,3],[4,5,6]] index = ['d','e'] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) df.ix[1] ''''' a 4 b 5 c 6 '''
3.2 經過行標籤索引
import pandas as pd data = [[1,2,3],[4,5,6]] index = ['d','e'] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) df.ix['e'] ''''' a 4 b 5 c 6 '''