We can use the DataFrame.drop() function to delete a row or a column from an existing DataFrame. In this article, we will discuss:
- How to delete rows from an existing DataFrame using Python pandas?
- How to delete columns from an existing DataFrame using Python pandas?
How to delete rows from an existing DataFrame using Python pandas?
We can use the following Python code to delete a row from an existing DataFrame using Python pandas.
import pandas list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] df1 = pandas.DataFrame(list1, index=["Row 1", "Row 2", "Row 3", "Row 4"], columns=["Column 1", "Column 2", "Column 3", "Column 4"]) print("df1 before deletion of rows: \n", df1) df1.drop(["Row 2", "Row 4"], axis=0, inplace=True) print("df1 after deletion of rows: \n", df1)
The output of the program will be:
df1 before deletion of rows: Column 1 Column 2 Column 3 Column 4 Row 1 1 2 3 4 Row 2 5 6 7 8 Row 3 9 10 11 12 Row 4 13 14 15 16 df1 after deletion of rows: Column 1 Column 2 Column 3 Column 4 Row 1 1 2 3 4 Row 3 9 10 11 12
Please note that the first parameter of the drop() function indicates the labels that should be deleted. The second parameter …






0 Comments