{"id":5613,"date":"2023-10-27T09:30:25","date_gmt":"2023-10-27T04:00:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=5613"},"modified":"2023-11-10T12:02:44","modified_gmt":"2023-11-10T06:32:44","slug":"drop-first-row-of-pandas-dataframe-3-ways","status":"publish","type":"post","link":"https:\/\/python-programs.com\/drop-first-row-of-pandas-dataframe-3-ways\/","title":{"rendered":"Drop first row of pandas dataframe (3 Ways)"},"content":{"rendered":"
In this article we will discuss about different ways to delete first row of pandas dataframe in Python.<\/p>\n
An Where,<\/p>\n So, we can select all the rows of the dataframe except the first row and assign back the selected rows to the original variable which will give an effect that the first row has been deleted from the dataframe.<\/p>\n To achieve this, select dataframe from row-2 and select all columns. Row-2 means we will select from index position-1 (as index position starts from 0 in dataframe) upto last row. And to select all columns use default values i.e ( i.e<\/p>\n So let’s see the implementation of it.<\/p>\n There is an drop() function in Panda’s dataframe which can be used to delete any rows from the dataframe.\u00a0 To make sure that rows only will be deleted then select So let’s see the implementation of it.<\/p>\n In python, dataframe provides a So let’s see the implementation of it.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" How to delete first row of pandas dataframe in Python ? In this article we will discuss about different ways to delete first row of pandas dataframe in Python. Method-1 : By using iloc attribute : An iloc attribute is there in Pandas by using which we can select a portion of the dataframe that …<\/p>\niloc<\/code> attribute is there in Pandas by using which we can select a portion of the dataframe that may be few columns or rows which is simply called as position based indexing.<\/p>\n
Syntax - df.<\/span>iloc<\/span>[start_row<\/span>:end_row , start_column, end_column<\/span>]<\/span><\/pre>\n
\n
:<\/code>)<\/p>\n
df = df.iloc<\/span>[<\/span>1<\/span>: , :<\/span>]<\/span><\/pre>\n
# Program :\r\n\r\nimport pandas as pd\r\n# List of tuples created\r\nempoyees = [('A',1,'a',10),\r\n ('B',2,'b',20),\r\n ('C',3,'c',30) ,\r\n ('D',4,'d',40)]\r\n# DataFrame object created\r\ndf = pd.DataFrame( empoyees, columns=['Upper', 'Smaller', 'Lower', 'Bigger'])\r\nprint(\"Contents of the original Dataframe : \")\r\nprint(df)\r\n# Dropping first row \r\ndf = df.iloc[1: , :]\r\nprint(\"Contents of modified Dataframe : \")\r\nprint(df)<\/pre>\n
Output :\r\nContents of the original Dataframe : \r\n Upper Smaller Lower Bigger\r\n0 A 1 a 10\r\n1 B 2 b 20\r\n2 C 3 c 30\r\n3 D 4 d 40\r\nContents of modified Dataframe : \r\n Upper Smaller Lower Bigger\r\n1 B 2 b 20\r\n2 C 3 c 30\r\n3 D 4 d 40<\/pre>\n
Method-2 : Using drop() function :<\/h3>\n
axis=0<\/code> and pass argument\u00a0
inplace=True<\/code>.<\/p>\n
# Program :\r\n\r\nimport pandas as pd\r\n# List of tuples created\r\nempoyees = [('A',1,'a',10),\r\n ('B',2,'b',20),\r\n ('C',3,'c',30) ,\r\n ('D',4,'d',40)]\r\n# DataFrame object created\r\ndf = pd.DataFrame( empoyees, columns=['Upper', 'Smaller', 'Lower', 'Bigger'])\r\nprint(\"Contents of the original Dataframe : \")\r\nprint(df)\r\n# Dropping first row \r\ndf.drop(index=df.index[0], \r\n axis=0, \r\n inplace=True)\r\n \r\nprint(\"Contents of modified Dataframe : \")\r\nprint(df)<\/pre>\n
Output : \r\nContents of the original Dataframe : \r\n\u00a0 \u00a0 Upper Smaller Lower Bigger\r\n0\u00a0 \u00a0 \u00a0A\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a01\u00a0 \u00a0 \u00a0 \u00a0 \u00a0a\u00a0 \u00a0 \u00a0 \u00a010\r\n1\u00a0 \u00a0 \u00a0B\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a02\u00a0 \u00a0 \u00a0 \u00a0 \u00a0b\u00a0 \u00a0 \u00a0 \u00a020\r\n2\u00a0 \u00a0 \u00a0C\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a03\u00a0 \u00a0 \u00a0 \u00a0 \u00a0c\u00a0 \u00a0 \u00a0 \u00a030\r\n3\u00a0 \u00a0 \u00a0D\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a04\u00a0 \u00a0 \u00a0 \u00a0 \u00a0d\u00a0 \u00a0 \u00a0 40\r\nContents of modified Dataframe : \r\n\u00a0 \u00a0 Upper Smaller Lower Bigger\r\n1\u00a0 \u00a0 \u00a0B\u00a0 \u00a0 \u00a0 \u00a0 \u00a02\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0b\u00a0 \u00a0 \u00a0 \u00a020\r\n2\u00a0 \u00a0 \u00a0C\u00a0 \u00a0 \u00a0 \u00a0 \u00a03\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0c\u00a0 \u00a0 \u00a0 \u00a030\r\n3\u00a0 \u00a0 \u00a0D\u00a0 \u00a0 \u00a0 \u00a0 4\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 d\u00a0 \u00a0 \u00a0 \u00a040<\/pre>\n
Method-3 : Using tail() function :<\/h3>\n
tail(n)<\/code> function which returns last ‘n’ rows. So to select all the rows except first row we can pass
tail(n-1)<\/code> which means first row deleted. And it assign back the selected rows to the original variable.<\/p>\n
# Program :\r\n\r\nimport pandas as pd\r\n# List of tuples created\r\nempoyees = [('A',1,'a',10),\r\n ('B',2,'b',20),\r\n ('C',3,'c',30) ,\r\n ('D',4,'d',40)]\r\n# DataFrame object created\r\ndf = pd.DataFrame( empoyees, columns=['Upper', 'Smaller', 'Lower', 'Bigger'])\r\nprint(\"Contents of the original Dataframe : \")\r\nprint(df)\r\n\r\n# Deleting first row by selecting last n-1 rows\r\ndf = df.tail(df.shape[0] -1)\r\n \r\nprint(\"Contents of modified Dataframe : \")\r\nprint(df)<\/pre>\n
Output :\r\nContents of the original Dataframe : \r\n\u00a0 \u00a0 \u00a0Upper Smaller Lower Bigger\r\n0\u00a0 \u00a0 \u00a0 A\u00a0 \u00a0 \u00a0 \u00a0 \u00a01\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 a\u00a0 \u00a0 \u00a0 \u00a0 \u00a010\r\n1\u00a0 \u00a0 \u00a0 B\u00a0 \u00a0 \u00a0 \u00a0 \u00a02\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 b\u00a0 \u00a0 \u00a0 \u00a0 \u00a020\r\n2\u00a0 \u00a0 \u00a0 C\u00a0 \u00a0 \u00a0 \u00a0 \u00a03\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 c\u00a0 \u00a0 \u00a0 \u00a0 \u00a030\r\n3\u00a0 \u00a0 \u00a0 D\u00a0 \u00a0 \u00a0 \u00a0 \u00a04\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 d\u00a0 \u00a0 \u00a0 \u00a0 40\r\nContents of modified Dataframe : \r\n\u00a0 \u00a0 Upper Smaller Lower Bigger\r\n1\u00a0 \u00a0 \u00a0B\u00a0 \u00a0 \u00a0 \u00a0 \u00a02\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 b\u00a0 \u00a0 \u00a0 \u00a020\r\n2\u00a0 \u00a0 \u00a0C\u00a0 \u00a0 \u00a0 \u00a0 \u00a03\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 c\u00a0 \u00a0 \u00a0 \u00a030\r\n3\u00a0 \u00a0 \u00a0D\u00a0 \u00a0 \u00a0 \u00a0 \u00a04\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 d\u00a0 \u00a0 \u00a0 \u00a040<\/pre>\n