In this article, you’ll learn how to delete all rows in Pandas DataFrame. The given examples with the solutions will help you to delete all the rows of Pandas DataFrame.
Method 1: Delete all rows of Pandas DataFrame
# Import the required libaries import pandas as pd # Create a DataFrame df = pd.DataFrame({'col_1': [30, 83, 89, 91], 'col_2': [30, 38, 28, 28], 'col_3': [39, 86, 18, 23]}) # Display the DataFrame print(df) # Delete all the Rows df = df[0:0] # Display the Modified DataFrame print(df)
Output:
col_1 col_2 col_3 0 30 30 39 1 83 38 86 2 89 28 18 3 91 28 23 Empty DataFrame Columns: [col_1, col_2, col_3] Index: []
Method 2:
# Import the required libaries import pandas as pd # Create a DataFrame df = pd.DataFrame({'col_1': [30, 83, 89, 91], 'col_2': [30, 38, 28, 28], 'col_3': [39, 86, 18, 23]}) # Display the DataFrame print(df) # Delete all the Rows df.drop(df.index,inplace=True) # Display the Modified DataFrame print(df)
Output:
col_1 col_2 col_3 0 30 30 39 1 83 38 86 2 89 28 18 3 91 28 23 Empty DataFrame Columns: [col_1, col_2, col_3] Index: []
Example: Delete all rows of the Dataset
# Import the required libaries import pandas as pd # Create a DataFrame df = pd.read_csv("weather.csv") # Display the DataFrame print(df.head()) # Delete all the Rows df = df[0:0] # Display the Modified DataFrame print(df)
Output:
date min_temp max_temp 0 01/01/2013 29.8 15.2 1 02/01/2013 32.1 16.8 2 03/01/2013 34.3 18.5 3 04/01/2013 33.1 17.6 4 05/01/2013 31.5 14.6 Empty DataFrame Columns: [date, min_temp, max_temp] Index: []
If you want to learn Pandas, I recommend this book and free resource.