Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Data Analytics with Pandas – How to Drop a List of Rows from a Pandas Dataframe

import pandas as pd

data = {"product_name":["Keyboard","Mouse", "Monitor", "CPU","CPU", "Speakers",pd.NaT],
        "Unit_Price":[500,200, 5000.235, 10000.550, 10000.550, 250.50,None],
        "No_Of_Units":[5,5, 10, 20, 20, 8,pd.NaT],
        "Available_Quantity":[5,6,10,"Not Available","Not Available", pd.NaT,pd.NaT],
        "Available_Since_Date":['11/5/2021', '4/23/2021', '08/21/2021','09/18/2021','09/18/2021','01/05/2021',pd.NaT]
       }

df = pd.DataFrame(data)

df
=================
df.drop([5,6], axis=0, inplace=True)

df

In this code,

    [5,6] is the index of the rows you want to delete
    axis=0 denotes that rows should be deleted from the dataframe
    inplace=True performs the drop operation in the same dataframe
#######
How to Drop Rows by Index Range in Pandas
df.drop(df.index[2:4], inplace=True)

df
==============
How to Drop All Rows after an Index in Pandas

df = df.iloc[:2]

df

In this code, :2 selects the rows until the index 2.
=================
How to Drop Rows with Multiple Conditions in Pandas

df.drop(df[(df['Unit_Price'] >400) & (df['Unit_Price'] < 600)].index, inplace=True)

df

In this code,

    (df['Unit_Price'] >400) & (df['Unit_Price'] < 600) is the condition to drop the rows.
    df[].index selects the index of rows which passes the condition.
    inplace=True performs the drop operation in the same dataframe rather than creating a new one.
====================

Comment

PREVIOUS NEXT
Code Example
Python :: number of libraries in python 
Python :: R[~i] in python 
Python :: FizzBuzz in Python Using Conditional Statements 
Python :: Insertion Sorting using while in python 
Python :: use dict to replace missing values pandas 
Python :: changing speak rate pyttsx 
Python :: print numbers 1 to 10 using recursion in python 
Python :: Set symmetric Using the Symmetric Difference Operator (^) Method 
Python :: find factors of a number using while loop 
Python :: Flatten List in Python Using NumPy Ravel 
Python :: Math Module fabs() Function in python 
Python :: display csv data into flask api 
Python :: python print numbers with commas 
Python :: tree view width 
Python :: how to stop a function from returning none 
Python :: docstring return list of tuple 
Python :: dataframe ggplot rownames order 
Python :: find not in dafatrame series 
Python :: Python NumPy ndarray flatten Function Example 02 
Python :: youtube-dl python not found 
Python :: Python NumPy asarray_chkfinite Function Syntax 
Python :: radar chart different scales python 
Python :: mid-point line drawing 
Python :: Exception has occurred: FileNotFoundError 
Python :: python multiply function with return keyword 
Python :: django admin auto update date field 
Python :: downsample audio 
Python :: Remove Brackets from List Using the * operator with the Separator method 
Python :: from android.runnable in python 
Python :: preallocate numpy array 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =