[pandas] Python reads a row or column of DataFrame

Row index, column index, loc and iloc

import pandas as pd
import numpy as np
# 准备数据
df = pd.DataFrame(np.arange(12).reshape(3,4),index=list("abc"),columns=list("WXYZ"))

insert image description here

  • Row index ( index ): corresponds to the leftmost vertical column

  • Column index ( columns ): corresponds to the top row

  • .loc[] Official interpretation: Access a group of rows and columns by label(s) or a boolean array. (Access a group of rows and columns by label or Boolean array) official link

    • loc uses index to get value, basic usage df.loc[[row index],[column index]]
  • .iloc[] official interpretation: Purely integer-location based indexing for selection by position. (index selection by position) official link

    • iloc uses the position (starting from 0) to take the value, the basic usage df.iloc[[row position],[column position]]

1. Get a column/multiple columns according to the column index (commonly used)

df['W']   # 取‘W’列,返回类型是Series
df[['W']] # 取‘W’列,返回类型是DataFrame
df[['W','Y']] # 取‘W’列和‘Y’列
df.loc[:,'W':'Y'] # 取‘W’列到‘Y’列

2. Get a row/multiple rows according to the row index

df.loc['a']   # 取‘a’行,返回类型是Series

df.loc[['a']]   # 取‘a’行,返回类型是DataFrame
df.loc[['a','c']]   # 取‘a’行和‘c’行,也可以写成 df.loc[['a','c'],:]
df.loc['a':'c',:]   # 取‘a’行到‘c’行

3. Take a column/multiple columns according to the column position

df.iloc[:,1]    # 取第2列(‘X’列),列号为1,返回类型是Series
df.iloc[:,0:2]  # 取前2列(‘W’列和‘X’列),列号为0和1
df.iloc[:,0:-1] # 取最后一列之前的所有列

4. Take a certain row/multiple rows according to the row position (commonly used)

df[:2]  #取前2行,行号为0和1
df[1:2] #取第2行,行号为1
df.iloc[1] # 取第2行(‘b’行),行号为1,返回类型是Series,也可以写成df.iloc[1,:]

5. Take a certain row and a certain column (commonly used)

df.loc['b','W'] # 取‘b’行‘W’列的值
df.iloc[0]['W'] # 取第1行、‘W’列的值

Six, take multiple rows and multiple columns

df[:2][['W','Y']]      # 取前2行的‘W’列和’Y‘列
df[:2].loc[:2,'W':'Y']  # 取前2行的‘W’列到’Y‘列

df.iloc[0][['W','Y']]  # 取第1行的‘W’列和’Y‘列
df.iloc[0]['W':'Y']    # 取第1行的‘W’列到’Y‘列

df.loc[["a","c"],["W","Y"]] # 取‘a’行和‘c’行,‘W’列和‘Y’列
df.iloc[[0,2],[1,3]]        # 取1、3行,2、4列

Summary: Generally, the row is fetched by the row position, and the column is fetched by the column index, and the row index is the same as the row position in most cases.
The most commonly used are the following

# 取某一列
df['W']
# 取某一行
df.iloc[0] 
# 取多列
df.loc[:,'W':'Y'] # 取‘W’列到‘Y’列
df.iloc[:,0:-1]   # 取最后一列之前的所有列
# 取对应行列的值
df.iloc[0]['W']
df.loc['a','W']  # 在行索引和行位置相同的情况下的写法就是,df.loc[0,'W']

Guess you like

Origin blog.csdn.net/qq_33218097/article/details/130315303