Using numpy to delete a row/column or multi-row content of DataFrame

Keywords: Database MySQL

I. Usage:

DataFrame.drop(labels=None,axis=0, index=None, columns=None, inplace=False)

Description of parameters:


labels: The name of the column to be deleted, given in the list
Axis: default is 0, which means deleting rows, so when deleting columns, specify axis=1;
index: Specify the row to be deleted directly
columns: directly specify the column to be deleted
inplace=False: By default, the deletion operation does not change the original data, but returns a new data frame after the deletion operation.
inplace=True: The original data will be deleted directly and cannot be returned after deletion.
 

2. There are two ways to delete rows and columns:


1) Combination of labels=None,axis=0
2) index or columns directly specify rows or columns to be deleted

 

Example:

>>>df = pd.DataFrame(np.arange(12).reshape(3,4), columns=['A', 'B', 'C', 'D'])
 
>>>df
 
   A   B   C   D
 
0  0   1   2   3
 
1  4   5   6   7
 
2  8   9  10  11
 
#Drop columns, two methods are equivalent
 
>>>df.drop(['B', 'C'], axis=1)
 
   A   D
 
0  0   3
 
1  4   7
 
2  8  11
 
>>>df.drop(columns=['B', 'C'])
 
   A   D
 
0  0   3
 
1  4   7
 
2  8  11
 
# In the first method, delete column must specify axis=1, otherwise it will report an error.
>>> df.drop(['B', 'C'])
 
ValueError: labels ['B' 'C'] not contained in axis
 
#Drop rows
>>>df.drop([0, 1])
 
   A  B   C   D
 
2  8  9  10  11
 
>>> df.drop(index=[0, 1])
 
   A  B   C   D
    
2  8  9  10  11

Delete the specified row:

>>> import pandas as pd
>>> df = {'DataBase':['mysql','test','test','test','test'],'table':['user','student','course','sc','book']}
>>> df = pd.DataFrame(df)
>>> df
  DataBase    table
0    mysql     user
1     test  student
2     test   course
3     test       sc
4     test     book
 
#Delete the row whose table value is sc
>>> df.drop(index=(df.loc[(df['table']=='sc')].index))
              
  DataBase    table
0    mysql     user
1     test  student
2     test   course
4     test     book

Delete multiple lines:

>>> df.drop(index=(df.loc[(df['DataBase']=='test')].index))
              
  DataBase table
0    mysql  user

 

Posted by hazel999 on Tue, 01 Oct 2019 14:13:54 -0700