This article is participating in Python Theme Month. See [activities]

Unlike the previous two articles, which dealt with row selection and column selection, this one deals with the use of row and column selection. The so-called simultaneous selection of rows and columns refers to the intersection of the result filtered by row and the result filtered by column, that is, the result of simultaneous selection by row and column.

The Excel screenshot used in this article is as follows:

First add a row index

Import pandas as pd df = pd.read_excel(r'C:\Users\admin\Desktop\ data analysis test.xlsx ') df.index = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'] print(df)Copy the code

result:

A Historical History Sima Qian B novel Dream of the Red Chamber Cao Xueqin C Prose Cultural Odyssey Yu Qiu Yu D History Ming Dynasty Those things in the bright moon E cartoon half an hour comic mixed Confucius said F essay Bacon Essay BACON G novel Dream of the Red Chamber Cao Xueqin H Prose Cultural Odyssey Yu Qiu Yu I history Ming Dynasty those things in those years bright moon J history of the state of the state of tongjian Sima Light K prose walker boundless yu Qiuyu L cartoon half an hour tang poetry mixed tzu yueCopy the code

1. Common index + Common index

In fact, it is the row name plus column name for data filtering

Df. Index = [' A ', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' and 'J' and 'K', 'L'] print (df) loc [[' A ', 'D'], [' title ']])Copy the code

result:

A Chronicles OF the Ming DynastyCopy the code

2. Location index + location index

It’s just the position of the row plus the position of the column to filter the data

Df. Index = [' A ', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' and 'J' and 'K', 'L'] print (df) loc [[' A ', 'D'], [' title ']])Copy the code
df.index = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
print(df.iloc[[0, 3], [1]])
Copy the code

result:

A Chronicles OF the Ming DynastyCopy the code

3. Boolean index + Normal index

The Boolean selection of the table selects rows, and then selects columns by normal indexes

Df. Index = [' A ', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' and 'J' and 'K', 'L'] print (df [df [' classification '] = = 'novel'] [[' title ', 'the authors']])Copy the code

result:

B dream of the Red Chamber cao Xueqin G Dream of the Red Chamber Cao XueqinCopy the code

4. Slice Index + Slice index

Is data selection by passing in a range of positions for both the row and column indexes

df.index = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
print(df.iloc[0:2, 1:3])
Copy the code

result:

Book title author A Shi Ji Sima Qian B Dream of the Red Chamber Cao XueqinCopy the code

Like python slicing, it includes the beginning but not the end