Change language

Python | Remove rows / columns from DataFrame using Pandas.drop ()

Pandas provide data analysts with a way to delete and filter a data frame using .drop() . With this method, rows or columns can be dropped using an index label or column name.

Syntax:
DataFrame.drop (labels = None, axis = 0, index = None, columns = None, level = None, inplace = False, errors = ’raise’)

Parameters:

labels: String or list of strings referring row or column name.
axis: int or string value, 0 ’index’ for Rows and 1 ’columns’ for Columns.
index or columns: Single label or list. index or columns are an alternative to axis and cannot be used together.
level: Used to specify level in case data frame is having multiple level index.
inplace: Makes changes in original Data Frame if True.
errors: Ignores error if any value from the list doesn’t exist and drops rest of the values ​​when errors = ’ignore’

Return type: Dataframe with dropped values ​​

To download the CSV used in the code, press here.

Example # 1: Deleting rows by index tag
the code is passed a list of index labels, and the lines matching those labels are removed using the .drop () method.

# pandas module import

import pandas as pd

 
# create a data frame from a CSV file

data = pd.read_csv ( " nba.csv " , index_col = " Name " )

 
# dropping missing values ​​

data.drop ([ "Avery Bradley" , "John Holland" , "RJ Hunter " ,

  " RJ Hunter " ], inplace = True )

< code class = "undefined spaces">  
# display
data

Output:
As shown in output images, the new output has no passed values. These values ​​were discarded and the changes were made to the original dataframe because inplace was True.

Dataframe before resetting values ​​

Data frame after deleting values ​​

Example # 2 : removing columns with column name

In its code, missing columns are removed using the column names.  axis is retained 1 because 1 refers to columns.

# pandas module import

import pandas as pd

 
# create data frame from CSV file

data = pd.read_csv ( "nba.csv" , index_col = "Name" )

 
# discard missing columns

data .drop ([ "Team" , "Weight"  ], axis = 1 , inplace = True )

 
# display
data

Output:
As shown in the output images, the new output is not has missing columns. These values ​​were discarded because the axis was set to 1 and changes were made to the original dataframe because inplace was True.

Dataframe before deleting columns

Data frame after deleting columns

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically