Home > Article > Backend Development > 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!