Home  >  Article  >  Backend Development  >  How to Remove Specific Rows from a Pandas Dataframe?

How to Remove Specific Rows from a Pandas Dataframe?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-31 14:04:01113browse

How to Remove Specific Rows from a Pandas Dataframe?

Dropping Specific Rows from a Pandas Dataframe

In data analysis, it is often necessary to remove specific rows from a dataframe. Pandas provides a straightforward method to accomplish this task, as demonstrated in the example below.

Consider a dataframe 'df' containing sales information:

<code class="python">df
             sales  discount  net_sales    cogs
STK_ID RPT_Date                                     
600141 20060331   2.709       NaN      2.709   2.245
       20060630   6.590       NaN      6.590   5.291
       20060930  10.103       NaN     10.103   7.981
       20061231  15.915       NaN     15.915  12.686
       20070331   3.196       NaN      3.196   2.710
       20070630   7.907       NaN      7.907   6.459</code>

To remove rows with row numbers given in a list (e.g., [1, 2, 4]), we can use the DataFrame.drop function. This function takes an index argument, which can be a Series of labels to drop. In this case, we create a list of the row numbers and convert it to a Series:

<code class="python">import pandas as pd

row_numbers = [1, 2, 4]
index_to_drop = pd.Series(row_numbers)</code>

Finally, we pass the index_to_drop Series to the DataFrame.drop function:

<code class="python">df = df.drop(index=index_to_drop)</code>

The resulting dataframe will only contain the rows that were not specified in the row_numbers list:

<code class="python">            sales  discount  net_sales    cogs
STK_ID RPT_Date                                     
600141 20060331   2.709       NaN      2.709   2.245
       20061231  15.915       NaN     15.915  12.686
       20070630   7.907       NaN      7.907   6.459</code>

The above is the detailed content of How to Remove Specific Rows from a Pandas Dataframe?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn