
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Delete rows/columns from DataFrame using Pandas.drop()
Pandas is one of the most popular python library for data analysis and data wrangling. In this article we will see how we can create a pandas dataframe and then delete some selective rows ort columns from this data frame.
Deleting roews
In the below example we have the iris.csv file which is read into a data frame. We first have a look at the existing data frame and then apply the drop function to the index column by supplying the value we want to drop. As we can see at the bottom of the result set the number of rows has been reduced by 3.
Example
import pandas as pd # making data frame from csv file data = pd.read_csv("E:\iris1.csv",index_col ="Id") print(data) # dropping passed values data.drop([6,9,10],inplace=True) # display print(data)
Output
Running the above code gives us the following result −
SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species Id 1 5.1 3.5 1.4 0.2 Iris-setosa 2 4.9 3.0 1.4 0.2 Iris-setosa 3 4.7 3.2 1.3 0.2 Iris-setosa . .. … .… .…..…… [150 rows x 5 columns] After Dropping SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species Id 1 5.1 3.5 1.4 0.2 Iris-setosa 2 4.9 3.0 1.4 0.2 Iris-setosa 3 4.7 3.2 1.3 0.2 Iris-setosa 149 6.2 3.4 5.4 2.3 Iris-virginica 150 5.9 3.0 5.1 1.8 Iris-virginica …………………. [147 rows x 5 columns]
Dropping Columns
For dropping the columns form a pandas data frame, we use the axis parameter. Its value is set to one in the drop function and we supply the column names to be dropped. As you can see the number of columns in the result set gets reduced from 5 to 3.
Example
import pandas as pd # making data frame from csv file data = pd.read_csv("E:\iris1.csv",index_col ="Id") print(data) # dropping passed values data.drop(['SepalWidthCm','PetalLengthCm'],axis=1,inplace=True) print("After Dropping") # display print(data)
Output
Running the above code gives us the following result −
SepalLengthCm SepalWidthCm PetalLengthCm PetalWidthCm Species Id 1 5.1 3.5 1.4 0.2 Iris-setosa 2 4.9 3.0 1.4 0.2 Iris-setosa 3 4.7 3.2 1.3 0.2 Iris-setosa . . .… .… .…. .…… [150 rows x 5 columns] After Dropping SepalLengthCm PetalWidthCm Species Id 1 5.1 0.2 Iris-setosa 2 4.9 0.2 Iris-setosa 3 4.7 0.2 Iris-setosa .....…. [150 rows x 3 columns]
- Related Articles
- Python Pandas – Count the rows and columns in a DataFrame
- How to display notnull rows and columns in a Python dataframe?
- Python - Drop specific rows from multiindex Pandas Dataframe
- Drop rows from Pandas dataframe with missing values or NaN in columns
- Python - Select multiple columns from a Pandas dataframe
- How to display all rows from dataframe using Pandas?
- Python Pandas - How to delete a row from a DataFrame
- Delete the first three rows of a DataFrame in Pandas
- Python Pandas - How to select multiple rows from a DataFrame
- Python Pandas - Display specific number of rows from a DataFrame
- Python Pandas - Select a subset of rows from a dataframe
- Python - Ranking Rows of Pandas DataFrame
- Python - How to drop the null rows from a Pandas DataFrame
- Python - Grouping columns in Pandas Dataframe
- Python Pandas - How to select rows from a DataFrame by integer location
