By using Show
Related: Drop DataFrame Rows by Checking Conditions In this article, I will cover how to remove rows by labels, by indexes, by ranges and how to drop 1. Pandas.DataFrame.drop() Syntax – Drop Rows & Columns
Let’s create a DataFrame, run some examples and explore the output. Note that our DataFrame contains index labels for rows which I am going to use to demonstrate removing rows by labels.
Yields below output
By default 2.1 Drop rows by Index Labels or NamesOne of the pandas advantages is you can assign labels/names to rows, similar to column names. If you have DataFrame with row labels (index labels), you can specify what rows you wanted to remove by label names.
Yields below output.
Alternatively, you can also write the same statement by using the field name
And by using
Notes:
2.2 Drop Rows by Index Number (Row Number)Similarly by using
Yields the same output as section 2.1. In order to remove the first row, you can use
2.3 Delete Rows by Index RangeYou can also remove rows by specifying the index range. The below example removes all rows starting 3rd row.
Yields below output.
2.4 Delete Rows when you have Default IndexsBy default pandas assign a sequence number to all rows also called index, row index starts from zero and increments by 1 for every row. If you are not using custom index labels then pandas DataFrame assigns sequence numbers as Index. To remove rows with the default index, you can try below.
Note that 2.5 Remove DataFrame Rows inplaceAll examples you have seen above return a copy
DataFrame after removing rows. In case if you wanted to remove rows inplace from referring DataFrame use
2.6 Drop Rows by Checking ConditionsMost of the time we would also need to remove DataFrame rows based on some conditions (column value), you can do this by using loc[] and iloc[] methods.
Yields below output.
2.7 Drop Rows that has NaN/None/Null ValuesWhile working with analytics you would often be required to clean up the data that has
This removes all rows that have None, Null & NaN values on any columns.
2.8 Remove Rows by Slicing DataFrameYou can also remove DataFrame rows by slicing. Remember index starts from zero.
You can also remove first N rows from pandas DataFrame and remove last N Rows from pands DataFrame Happy Learning !! ConclusionIn this pandas drop rows article you have learned how to drop/remove pandas DataFrame rows using drop() method. By default drop() deletes rows (axis = 0), if you wanted to delete columns either you have to use axis =1 or columns=labels param. Also Read
References
How do you delete a data frame in Python?Pandas DataFrame drop() Method
The drop() method removes the specified row or column. By specifying the column axis ( axis='columns' ), the drop() method removes the specified column. By specifying the row axis ( axis='index' ), the drop() method removes the specified row.
How do I delete DataFrame data in pandas?To delete a row from a DataFrame, use the drop() method and set the index label as the parameter.
How do you delete DataFrame entries?drop() method you can drop/remove/delete rows from DataFrame. axis param is used to specify what axis you would like to remove. By default axis = 0 meaning to remove rows. Use axis=1 or columns param to remove columns.
How do you delete all data from a DataFrame in Python?Delete rows and columns from a DataFrame using Pandas drop(). Delete a single row.. Delete multiple rows.. Delete rows based on row position and custom range.. Delete a single column.. Delete multiple columns.. Delete columns based on column position and custom range.. Working with MultiIndex DataFrame.. |