How to Drop Rows in PandasUsing pandas.DataFrame.drop() we may drop, remove, or eliminate rows we want from the given DataFrame. We may choose the axis to delete using the DataFrame.axis param. Axis = 0 by default means to eliminate rows. To eliminate columns, apply axis = 1 or columns_param. When removing rows, Pandas, by default, creates a copy of the DataFrame; to delete from an existing referencing DataFrame, use the inplace = True option. Syntax: Parameters:
Return type: Updated DataFrame Delete a Single Row Using the Row Index LabelOne advantage of pandas is providing rows labels or titles similar to column names. We can specify individual rows to remove using the row label names if our data frame supports row labeling (also called index labeling). Code Output: Original DataFrame: Name Age Salary (LPA) A Itika 21 32 B Peter 26 20 C Harry 28 38 D Naill 25 17 New DataFrame: Name Age Salary (LPA) A Itika 21 32 C Harry 28 38 D Naill 25 17 Delete Multiple Rows Using Index LabelsWe can give multiple row-index labels in a list to the drop command to delete multiple rows from the DataFrame. Code Output: Original DataFrame: Name Age Salary (LPA) A Itika 21 32 B Peter 26 20 C Harry 28 38 D Naill 25 17 New DataFrame: Name Age Salary (LPA) A Itika 21 32 D Naill 25 17 Drop Rows by Index NumberLikewise, we can delete rows from a given pandas DataFrame by providing index position as arguments to the drop() method. We must extract the row names using the indices and pass them to the drop() method since it does not accept the position index of rows as a parameter. To obtain row labels of a DataFrame for the indices we intended to delete, we will use df.index. All row labels are returned as a list using the df.index.values function. For instance, using df.index[[1,2]], the row labels for the second and third rows are obtained; the drop() method then deletes these rows. Keep in mind that the indexing of lists in Python starts at zero. Code Output: Original DataFrame: Name Age Salary (LPA) A Itika 21 32 B Peter 26 20 C Harry 28 38 D Naill 25 17 New DataFrame: Name Age Salary (LPA) A Itika 21 32 D Naill 25 17 Delete rows of a DataFrame in PlaceUntil now, we have been getting a new DataFrame with the rows deleted. However, we can delete the rows of a DataFrame without creating a new one called performing 'in place' operations on the DataFrame. Code Output: Original DataFrame: Name Age Salary (LPA) A Itika 21 32 B Peter 26 20 C Harry 28 38 D Naill 25 17 Updated DataFrame: Name Age Salary (LPA) A Itika 21 32 D Naill 25 17 Delete Rows Using Index RangeBy defining the index range, we may also eliminate rows. The instance below deletes all rows that exist before the third row. Code Output: Original DataFrame: Name Age Salary (LPA) A Itika 21 32 B Peter 26 20 C Harry 28 38 D Naill 25 17 Updated DataFrame: Name Age Salary (LPA) D Naill 25 17 Drop Rows by Checking ConditionsThrough the loc[] and iloc[] functions, we can easily remove DataFrame rows according to certain conditions (column value). Code Output: Original DataFrame: Name Age Salary (LPA) A Itika 21 32 B Peter 26 20 C Harry 28 38 D Naill 25 17 New DataFrame: Name Age Salary (LPA) A Itika 21 32 Drop Rows Containing NaN/None ValuesWe will frequently need to clean up rows that contain None, Null, and np.NaN values when dealing with analytics. We can delete NaN values from a given DataFrame by calling df.dropna(). Code Output: Original DataFrame: Name Age Salary (LPA) A Itika 21 32.0 B Peter 26 NaN C Harry 28 38.0 D Naill 25 NaN New DataFrame: Name Age Salary (LPA) A Itika 21 32.0 C Harry 28 38.0 Next Topic# |