In this article, you’ll learn how to delete the first three rows of Pandas DataFrame. Three methods are given to delete the first three rows of Pandas DataFrame.
Method 1: Delete the First Three rows using iloc
# Import the Pandas library as pd import pandas as pd # Create a DataFrame df = pd.DataFrame({'col_1': [15, 37, 26, 30], 'col_2': [10, 58, 28, 38], 'col_3': [20, 54, 34, 34]}) # Display the DataFrame print(df) # Delete first three rows of a DataFrame df = df.iloc[3:] # Display the Modified DataFrame print(df)
Output:
col_1 col_2 col_3 0 15 10 20 1 37 58 54 2 26 28 34 3 30 38 34 col_1 col_2 col_3 3 30 38 34
Method 2
# Import the Pandas library as pd import pandas as pd # Create a DataFrame df = pd.DataFrame({'col_1': [15, 37, 26, 30], 'col_2': [10, 58, 28, 38], 'col_3': [20, 54, 34, 34]}) # Display the DataFrame print(df) # Delete first three rows of a DataFrame df.drop(df.index[:3], inplace=True) # Display the Modified DataFrame print(df)
col_1 col_2 col_3 0 15 10 20 1 37 58 54 2 26 28 34 3 30 38 34 col_1 col_2 col_3 3 30 38 34
Method 3
# Import the Pandas library as pd import pandas as pd # Create a DataFrame df = pd.DataFrame({'col_1': [15, 37, 26, 30], 'col_2': [10, 58, 28, 38], 'col_3': [20, 54, 34, 34]}) # Display the DataFrame print(df) # Delete first three rows of a DataFrame df = df.tail(df.shape[0]- 3) # Display the Modified DataFrame print(df)
col_1 col_2 col_3 0 15 10 20 1 37 58 54 2 26 28 34 3 30 38 34 col_1 col_2 col_3 3 30 38 34
If you want to learn Pandas, I recommend this book and free resource.