一 通过索引取数据 (ix/loc/iloc)

loc (根据索引名称取数据)

iloc (根据索引序号取数据)

ix  (综合两者)

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']
#----------------------------------------------------------------------------

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

#--------------------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, 使用名称查找

相关文章:

  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
  • 2021-10-20
  • 2022-01-31
  • 2021-08-04
猜你喜欢
  • 2021-05-18
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2021-09-09
  • 2022-01-07
相关资源
相似解决方案