![]() |
VOOZH | about |
A DataFrame is given and the task is to remove the first row from it. For example, consider the following DataFrame:
Input: A B
0 1 2
1 3 4Output: A B
1 3 4
Let's explore different methods to drop first row.
iloc is used to select data based on integer (position-based) indexing in a DataFrame. It allows to access rows and columns using their index positions. To drop the first row using iloc, select all rows starting from index 1:
name age 1 Kate 22 2 Nancy 21 3 Emilia 22
Explanation:
In this method, the index of the row (typically 0) is specified and the drop() method is used with the index parameter set to 0. To modify the original DataFrame directly, the inplace=True argument is used.
name age 1 Kate 22 2 Nancy 21 3 Emilia 22
Explanation:
loc is used to drop the first row by selecting data based on index labels instead of positions. By starting the selection from the second label onward, it excludes the first row from the DataFrame.
name age 20 Kate 22 30 Nancy 21 40 Emilia 22
Explanation:
The tail() function is used to drop the first row by selecting all rows except the first based on count. By taking the last n-1 rows, it excludes the first row from the DataFrame.
name age 1 Kate 22 2 Nancy 21 3 Emilia 22
Explanation: