DataFrame查找

一 經過索引取數據 (ix/loc/iloc)性能

loc (根據索引名稱取數據 , 適合多列)spa

iloc (根據索引序號取數據,   適合多列)code

at  (和loc相似,只用於取單列, 性能更好)blog

iat (和iloc相似,只用於取單列,性能更好)索引

ix  (綜合上面)class

data = [[1,2,3],[4,5,6]]
index = ['A','B']
columns=['a','b','c']
df = pd.DataFrame(data=data, index=index, columns=columns)

#--------------------Loc的用法-----------------------------------------------
# 取第1行
print df.loc['A']
# 取第1行列名 'b'
print df.loc['A', ['b']]
# 取多列
print df.loc['A', ['b', 'c']]
#----------------------------------------------------------------------------

#--------------------iLoc的用法-----------------------------------------------
# 取第1行
print df.iloc[0]
# 取第1行列名 'b'
print df.iloc[0, [1]]
# 取多列
print df.iloc[0, [1, 2]]
#----------------------------------------------------------------------------

#--------------------at的用法-----------------------------------------------
print df.at["A", 'a']
#---------------------------------------------------------------------------

#--------------------iat的用法-----------------------------------------------
print df.iat[0, 0]
#----------------------------------------------------------------------------

#--------------------ix的用法-----------------------------------------------
# 取第1行
print df.ix[0]
# 取第1行列名 'b'
print df.ix[0][1]

# 取第1行
print df.ix['A']
# 取第1行列名 'b'
print df.ix['A']['b']
#----------------------------------------------------------------------------

  須要注意的地方,1 該類用法必須先經過索引,取到行(series)再取列數據, 直接取列數據會報錯  2 經過ix獲取數據時,若是索引爲int, 則識別爲loc, 使用名稱查找數據

二  獲取索引和字段名di

#--------------------獲取索引-----------------------------------------------
print df.index[0]
#--------------------------------------------------------------------------

#--------------------獲取列名-----------------------------------------------
print df.columns[0]
#--------------------------------------------------------------------------
相關文章
相關標籤/搜索