In this article, you’ll learn how to delete the first two rows in Pandas. Three methods are given to delete the first two rows of Pandas DataFrame.
Method 1: Delete the first two rows using iloc
# Import the Pandas library as pd import pandas as pd # Create a DataFrame df = pd.DataFrame({'col_1': [65, 60, 16, 60], 'col_2': [80, 28, 38, 48], 'col_3': [40, 64, 94, 54]}) # Display the DataFrame print(df) # Delete first two rows of a DataFrame df = df.iloc[2:] # Display the Modified DataFrame print(df)
Output:
col_1 col_2 col_3 0 65 80 40 1 60 28 64 2 16 38 94 3 60 48 54 col_1 col_2 col_3 2 16 38 94 3 60 48 54
Method 2:
# Import the Pandas library as pd import pandas as pd # Create a DataFrame df = pd.DataFrame({'col_1': [65, 60, 16, 60], 'col_2': [80, 28, 38, 48], 'col_3': [40, 64, 94, 54]}) # Display the DataFrame print(df) # Delete first two rows of a DataFrame df.drop(df.index[:2], inplace=True) # Display the Modified DataFrame print(df)
Output:
col_1 col_2 col_3 0 65 80 40 1 60 28 64 2 16 38 94 3 60 48 54 col_1 col_2 col_3 2 16 38 94 3 60 48 54
Method 3:
# Import the Pandas library as pd import pandas as pd # Create a DataFrame df = pd.DataFrame({'col_1': [65, 60, 16, 60], 'col_2': [80, 28, 38, 48], 'col_3': [40, 64, 94, 54]}) # Display the DataFrame print(df) # Delete first two rows of a DataFrame df = df.tail(df.shape[0]- 2) # Display the Modified DataFrame print(df)
Output:
col_1 col_2 col_3 0 65 80 40 1 60 28 64 2 16 38 94 3 60 48 54 col_1 col_2 col_3 2 16 38 94 3 60 48 54
If you want to learn Pandas, I recommend this book and free resource.