3. Add, delete, modify and query dataframe

Time:2021-1-24

Take this as an example

1、 Preliminary cognition of dataframe

After reading the data in panda, the data is saved in dataframe. In the operation to understand the dataframe function

function meaning Examples
values element
index Indexes
columns Listing
dtyoes type
size Number of elements
ndim Dimension number
shape Data shape 4479c2ffd3ddd50a190956c76593ae1e.png

2、 Dataframe query

1. Single column view

Dataframe ['column name ']

Examples

2. Rows in a single column

Dataframe ['column name '] [start value: end value]

Examples

3. Multiple rows and columns

Dataframe [[column name, column name]] [starting value: final value]

4. LOC query

DataFrame.Loc [row, column name]

Examples

5. Iloc query

DataFrame.iloc [row, column order]

Examples

6. Condition query

X [x [‘index name ‘] judgment condition]

Dataframe [dataframe attribute + judgment condition]

Examples
9c50e2e75d7493014cce969b88c125d9.png

3、 Dataframe change

1. Column name change

(1) Replace change

DataFrame.rename (columns = {original name ':'current name'})

(2) Override changes

DataFrame.columns= {column name 1 ','column name 2',... 'column name n'}

Examples

2. Index change

DataFrame.reset_index()

Examples

3. Index change

Dataframe [index] = assignment

Examples

4、 Increase

1. Add columns

Dataframe ['column name to be added '] = assignment
**Examples**

2. Add lines

DataFrame.loc [to add row] = {column name ':'added content'...}

Examples

5、 Delete

drop

Drop (labels ='column name ', axis = 0 / 1)

1. Column deletion

Drop (labels ='column name ', axis = 1)

Examples

2. Line deletion

Drop (labels = [line order], axis = 0)

Examples