How to drop rows in python
To delete a row from a DataFrame, use the drop() method and set the index label as the parameter. Show At first, let us create a DataFrame. We have index label as w, x, y, and z: dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]],index=['w', 'x', 'y', 'z'], columns=['a', 'b']) Now, let us use the index label and delete a row. Here, we will delete a row with index label ‘w’ dataFrame = dataFrame.drop('w') ExampleFollowing is the code import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]],index=['w', 'x', 'y', 'z'],columns=['a', 'b']) # DataFrame print"DataFrame...\n",dataFrame # deleting a row dataFrame = dataFrame.drop('w') print"DataFrame after deleting a row...\n",dataFrame OutputThis will produce the following output DataFrame... a b w 10 15 x 20 25 y 30 35 z 40 45 DataFrame after deleting a row... a b x 20 25 y 30 35 z 40 45
Updated on 14-Sep-2021 08:16:16
In this tutorial we will learn how to drop or delete the row in python pandas by index, delete row by condition in python pandas and drop rows by position. Dropping a row in pandas is achieved by using .drop() function. Lets see example of each. the dataframe will be Simply drop a row or observation:Dropping the second and third row of a dataframe is achieved as follows # Drop an observation or row df.drop([1,2]) The above code will drop the second and third row. Drop a row or observation by condition:we can drop a row when it satisfies a specific condition # Drop a row by condition df[df.Name != 'Alisa'] The above code takes up all the names except Alisa, thereby dropping the row with name ‘Alisa’. So the resultant dataframe will be Drop a row or observation by index:We can drop a row by index as shown below # Drop a row by index df.drop(df.index[2]) The above code drops the row with index number 2. So the resultant dataframe will be Drop the row by position:Now let’s drop the bottom 3 rows of a dataframe as shown below # Drop bottom 3 rows df[:-3] The above code selects all the rows except bottom 3 rows, there by dropping bottom 3 rows, so the resultant dataframe will be Drop Duplicate rows of the dataframe in pandasnow lets simply drop the duplicate rows in pandas as shown below # drop duplicate rows df.drop_duplicates() In the above example first occurrence of the duplicate row is kept and subsequent duplicate occurrence will be deleted, so the output will be For further detail on drop duplicates one can refer our page on Drop duplicate rows in pandas python drop_duplicates() Drop rows with NA values in pandas pythonDrop the rows even with single NaN or single missing values. df.dropna() so the resultant table on which rows with NA values dropped will be Outputs: For further detail on drop rows with NA values one can refer our page Other related topics :
for documentation on drop() function kindly refer here How do you drop multiple rows in Python?Delete a Multiple Rows by Index Position in DataFrame
As df. drop() function accepts only list of index label names only, so to delete the rows by position we need to create a list of index names from positions and then pass it to drop(). As default value of inPlace is false, so contents of dfObj will not be modified.
How do I drop a row in Pandas Python?To drop a row or column in a dataframe, you need to use the drop() method available in the dataframe. You can read more about the drop() method in the docs here. Rows are labelled using the index number starting with 0, by default. Columns are labelled using names.
How do you delete a row in Python?To delete a row from a DataFrame, use the drop() method and set the index label as the parameter.
How do you drop data in Python?Python pandas drop rows by index
To remove the rows by index all we have to do is pass the index number or list of index numbers in case of multiple drops. to drop rows by index simply use this code: df. drop(index) . Here df is the dataframe on which you are working and in place of index type the index number or name.
|