How can I remove rows from a Pandas DataFrame?

How can I remove rows from a Pandas DataFrame?

To remove rows from a Pandas DataFrame, use the .drop() function and specify the index or labels of the rows to be removed. This function will return a new DataFrame with the specified rows removed. Alternatively, you can use the .drop() function in place by setting the “inplace=True” parameter. This will modify the original DataFrame. Additionally, you can use conditional statements to filter out rows that meet a certain criteria and create a new DataFrame with the remaining rows.

Pandas: Pop Rows from DataFrame


You can use the pop() function to quickly remove a column from a pandas DataFrame.

In order to use the pop() function to remove rows, you must first transpose the DataFrame and then use the pop() function to remove the columns (i.e. the rows of the original DataFrame):

#pop the row in index position 3
df.T.pop(3)

The following example shows how to use this syntax in practice.

Example: Pop Rows from pandas DataFrame

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F'],
                   'points': [18, 22, 19, 14, 14, 11],
                   'assists': [5, 7, 7, 9, 12, 9]})

#view DataFrame
print(df)

  team  points  assists
0    A      18        5
1    B      22        7
2    C      19        7
3    D      14        9
4    E      14       12
5    F      11        9

Now suppose we would like to remove the row in index position 3 of the DataFrame.

We can transpose the DataFrame and then use the pop() function to remove the row in index position 3:

#define transposed DataFrame
df_transpose = df.T#remove row in index position 3 of original DataFrame
df_transpose.pop(3)

team        D
points     14
assists     9
Name: 3, dtype: object

We can then transpose the DataFrame once again to get back the original DataFrame with one row removed:

#transpose back to original DataFrame
df = df_transpose.T#view updated DataFrame
print(df)

  team points assists
0    A     18       5
1    B     22       7
2    C     19       7
4    E     14      12
5    F     11       9

Notice that the row in index position 3 has been removed from the DataFrame.

All other rows in the DataFrame remain untouched.

Note: You can find the complete documentation for the pop() function in pandas .

The following tutorials explain how to perform other common tasks in pandas:

Cite this article

stats writer (2024). How can I remove rows from a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-remove-rows-from-a-pandas-dataframe/

stats writer. "How can I remove rows from a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-rows-from-a-pandas-dataframe/.

stats writer. "How can I remove rows from a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-remove-rows-from-a-pandas-dataframe/.

stats writer (2024) 'How can I remove rows from a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-remove-rows-from-a-pandas-dataframe/.

[1] stats writer, "How can I remove rows from a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I remove rows from a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top