How to drop ROW and COLUMN in a Pandas DataFrame in Python

Hits: 161

How to drop ROW and COLUMN in a Pandas DataFrame in Python

To drop a row or column in a Pandas DataFrame in Python, you can use the drop method. The drop method takes two main arguments: the index or labels of the rows/columns to be removed, and the axis (0 for rows and 1 for columns) on which the operation should be performed.

For example, if you want to drop the third row in a DataFrame df, you would use the following code:

df.drop(2, axis=0, inplace=True)

 

This will remove the third row from the DataFrame, and the inplace=True argument is used to make the change permanent.

Similarly, If you want to drop a column from a DataFrame, you can pass the column label in the drop() method with axis=1

df.drop("column_name", axis=1, inplace=True)

 

Note that the drop() method does not modify the original DataFrame by default, so if you want the changes to be permanent, you should set the inplace parameter to True.

Also to drop multiple rows and columns, you can pass the list of index/label as input in the drop method, this will drop all the rows/columns in the list.

df.drop([2,3], axis=0, inplace=True)
df.drop(["column1","column2"], axis=1, inplace=True)

 

It’s important to be careful when removing rows or columns from a DataFrame, as it can have a significant impact on the results of any further analysis that is performed on the data.

In this Learn through Codes example, you will learn: How to drop ROW and COLUMN in a Pandas DataFrame in Python.



 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

 

Data Structure in Python | Jupyter Notebook | Python Data Science for beginners

 

How to get descriptive statistics of a Pandas DataFrame in Python