index是行索引,即每一行的名字;columns是列索引,即每一列的名字。创建数据帧时行索引和列索引都须要以列表的形式传入。数组
import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], index=['row_0', 'row_1'], columns=['col_0', 'col_1', 'col_2'])
# 以数组形式返回 row_name = df.index.values
# 以列表形式返回 row_name = df.index.values.tolist()
# 以数组的形式返回 col_name = df.columns.values
# 以列表的形式返回 col_name = df.columns.values.tolist()
获取某行数据需用.loc[]或.iloc[]方法,不能直接索引。spa
# 以行名索引,返回一个系列(series) df_row0 = df.loc['row_0']
# 以行的绝对位置索引,返回一个系列(series) df_row0 = df.iloc[0]
获取某列数据能够经过列名直接索引。code
# 以列名索引,返回一个系列(series) df_col0 = df['col_0']
索引某列不能直接经过列的绝对位置来索引,但能够转换思路,借助列索引值实现用绝对位置的间接索引。blog
# df_col0 = df[0] 经过绝对位置直接索引报错 # 经过列索引名 df.columns 实现对列的绝对位置索引 df_col0 = df[df.columns[0]]
对行进行切片操做,能够经过.iloc[]方法或直接用行的绝对位置。不能经过行名进行切片操做。索引
# 经过iloc[]方法切片,[0:2]左闭右开,即切取第0行和第1行 df_row = df.iloc[0:2]
# 经过行的绝对位置切片,[0:2]左闭右开,即切取第0行和第1行 df_row = df[0:2]
对列进行切片时,能够将所须要切取的列的列名组成一个一维的列表或数组,直接传入df[]便可。pandas
# df_col = df[df.columns[0:2]] 切取第0列和第1列,与下句代码等价 df_col = df[['col_0', 'col_1']]
先进行行切片,再进行列切片便可。class
# 切取第0行和第1行,'col_0'和'col_2'列 df_new = df[0:2][['col_0', 'col_2']]
# 经过行列定位,返回值为一个系列(series) df_new = df.loc['row_0'][['col_0']]
# 用行名和列名索引,返回该位置的具体元素 df_new = df.at['row_0', 'col_0']
# 用行列的绝对位置定位,返回该位置的具体元素 df_new = df.iat[0,0]
小结:对行操做通常经过df.iloc[绝对位置]或df.loc[‘行名’],对列操做直接用df[‘列名’]import