{"id":4885,"date":"2021-05-01T15:03:14","date_gmt":"2021-05-01T09:33:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=4885"},"modified":"2021-11-22T18:42:58","modified_gmt":"2021-11-22T13:12:58","slug":"python-pandas-how-to-drop-rows-in-dataframe-by-index-labels","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-pandas-how-to-drop-rows-in-dataframe-by-index-labels\/","title":{"rendered":"Python Pandas : How to drop rows in DataFrame by index labels"},"content":{"rendered":"
In this article we are going to learn how to delete single or multiple rows from a Dataframe.<\/p>\n
For this we are going to use the Where, the function accepts name\/series of names in the label and deletes the rows or columns it points to. The axis is used to alter between rows and columns, 0 means rows and 1 means columns(default value is 0).<\/p>\n Also we have to pass We are going to use the following dataset as example<\/p>\n To delete a single row by the label we can just pass the label into the function.<\/p>\n Here let\u2019s try to delete Output :<\/p>\n To delete multiple rows by their labels we can just pass the labels into the function inside a square bracket Here let\u2019s try to delete To delete multiple rows we know the index position, however, the function Want to expert in the python programming language? Exploring\u00a0Python Data Analysis using Pandas<\/a>\u00a0tutorial changes your knowledge from basic to advance level in python concepts.<\/p>\n Read more Articles on Python Data Analysis Using Padas \u2013 Remove Contents from a Dataframe<\/strong><\/p>\n How to drop rows in DataFrame by index labels in Python ? In this article we are going to learn how to delete single or multiple rows from a Dataframe. For this we are going to use the drop( ) function. Syntax – DataFrame.drop( labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=’raise’ ) Where, the function …<\/p>\ndrop( )<\/code> function.<\/p>\n
Syntax - DataFrame.drop( labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise' )<\/pre>\n
inplace = True<\/code><\/em> if we want the modified values to be updated in our dataframe object, as the
drop( )<\/code> function returns the modified values into a new Dataframe object. To explain this properly we will be using
inplace = True<\/code> <\/em>in all our programs.<\/p>\n
Name\u00a0 Age\u00a0\u00a0\u00a0\u00a0\u00a0 Location Country\r\na\u00a0\u00a0\u00a0\u00a0 Jill\u00a0\u00a0 16\u00a0\u00a0\u00a0\u00a0 Tokyo\u00a0\u00a0 Japan\r\nb\u00a0\u00a0 Phoebe\u00a0\u00a0 38\u00a0 New York\u00a0\u00a0\u00a0\u00a0 USA\r\nc\u00a0\u00a0\u00a0 Kirti\u00a0\u00a0 39\u00a0 New York\u00a0\u00a0\u00a0\u00a0 USA\r\nd\u00a0\u00a0\u00a0 Veena\u00a0\u00a0 40\u00a0\u00a0\u00a0\u00a0 Delhi\u00a0\u00a0 India\r\ne\u00a0\u00a0\u00a0\u00a0 John\u00a0\u00a0 54\u00a0\u00a0\u00a0 Mumbai\u00a0\u00a0 India\r\nf\u00a0 Michael\u00a0\u00a0 21\u00a0\u00a0\u00a0\u00a0 Tokyo\u00a0\u00a0 Japan<\/pre>\n
Deleting a single Row in DataFrame by Row Index Label :<\/h3>\n
\u2018b\u2019 row<\/code><\/code>.<\/p>\n
#program :\r\n\r\nimport numpy as np\r\nimport pandas as pd\r\n\r\n#Examole data\r\nstudents = [('Jill',\u00a0\u00a0 16,\u00a0 'Tokyo',\u00a0\u00a0\u00a0\u00a0 'Japan'),\r\n('Phoebe', 38,\u00a0 'New York',\u00a0 'USA'),\r\n('Kirti',\u00a0 39,\u00a0 'New York',\u00a0 'USA'),\r\n('Veena',\u00a0 40,\u00a0 'Delhi',\u00a0\u00a0\u00a0\u00a0 'India'),\r\n('John',\u00a0\u00a0 54,\u00a0 'Mumbai',\u00a0\u00a0\u00a0 'India'),\r\n(\"Michael\",21,\u00a0 'Tokyo',\u00a0\u00a0\u00a0\u00a0 'Japan')]\r\n\r\n#Creating an object of dataframe class\r\ndfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'Location' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f'])\r\n#Deleting 'b' row\r\ndfObj.drop('b',inplace=True)\r\nprint(dfObj)<\/pre>\n
Name\u00a0 Age\u00a0\u00a0\u00a0\u00a0\u00a0 Location Country\r\na\u00a0\u00a0\u00a0\u00a0 Jill\u00a0\u00a0 16\u00a0\u00a0\u00a0\u00a0 Tokyo\u00a0\u00a0 Japan\r\nc\u00a0\u00a0\u00a0 Kirti\u00a0\u00a0 39\u00a0 New York\u00a0\u00a0\u00a0\u00a0 USA\r\nd\u00a0\u00a0\u00a0 Veena\u00a0\u00a0 40\u00a0\u00a0\u00a0\u00a0 Delhi\u00a0\u00a0 India\r\ne\u00a0\u00a0\u00a0\u00a0 John\u00a0\u00a0 54\u00a0\u00a0\u00a0 Mumbai\u00a0\u00a0 India\r\nf\u00a0 Michael\u00a0\u00a0 21\u00a0\u00a0\u00a0\u00a0 Tokyo\u00a0\u00a0 Japan<\/pre>\n
Deleting Multiple Rows in DataFrame by Index Labels :<\/h3>\n
[ ]<\/code>.<\/p>\n
'a'<\/code> and
'b'<\/code>\u00a0row.<\/p>\n
#program :\r\n\r\nimport numpy as np\r\nimport pandas as pd\r\n\r\n#Examole data\r\nstudents = [('Jill',\u00a0\u00a0 16,\u00a0 'Tokyo',\u00a0\u00a0\u00a0\u00a0 'Japan'),\r\n('Phoebe', 38,\u00a0 'New York',\u00a0 'USA'),\r\n('Kirti',\u00a0 39,\u00a0 'New York',\u00a0 'USA'),\r\n('Veena',\u00a0 40,\u00a0 'Delhi',\u00a0\u00a0\u00a0\u00a0 'India'),\r\n('John',\u00a0\u00a0 54,\u00a0 'Mumbai',\u00a0\u00a0\u00a0 'India'),\r\n(\"Michael\",21,\u00a0 'Tokyo',\u00a0\u00a0\u00a0\u00a0 'Japan')]\r\n\r\n#Creating an object of dataframe class\r\ndfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'Location' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f'])\r\n\r\n#Deleting 'a' and 'b' row\r\ndfObj.drop(['a','b'],inplace=True)\r\nprint(dfObj)<\/pre>\n
Output :\r\n \u00a0 \u00a0 Name\u00a0 \u00a0 \u00a0 \u00a0Age\u00a0\u00a0\u00a0\u00a0\u00a0Location\u00a0 \u00a0 \u00a0 \u00a0Country\r\nc\u00a0 \u00a0 \u00a0Kirti\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 39\u00a0 \u00a0 \u00a0 New York\u00a0 \u00a0 \u00a0 USA\r\nd\u00a0 \u00a0 \u00a0Veena\u00a0 \u00a0 \u00a0 40\u00a0 \u00a0 \u00a0 Delhi\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0India\r\ne\u00a0\u00a0\u00a0\u00a0 John\u00a0 \u00a0 \u00a0 \u00a0 \u00a054\u00a0 \u00a0 \u00a0Mumbai\u00a0 \u00a0 \u00a0 \u00a0 \u00a0India\r\nf\u00a0 \u00a0 \u00a0Michael\u00a0 \u00a0 \u00a021\u00a0\u00a0\u00a0\u00a0 Tokyo\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Japan<\/pre>\n
Deleting Multiple Rows by Index Position in DataFrame :<\/h3>\n
drop( )<\/code> doesn\u2019t take indices as parameters. So we create the list of labels and pass them into the
drop( )<\/code> function. Let\u2019s try deleting the same rows again but by index.<\/p>\n
#program :\r\n\r\nimport numpy as np\r\nimport pandas as pd\r\n\r\n#Examole data\r\nstudents = [('Jill',\u00a0\u00a0 16,\u00a0 'Tokyo',\u00a0\u00a0\u00a0\u00a0 'Japan'),\r\n('Phoebe', 38,\u00a0 'New York',\u00a0 'USA'),\r\n('Kirti',\u00a0 39,\u00a0 'New York',\u00a0 'USA'),\r\n('Veena',\u00a0 40,\u00a0 'Delhi',\u00a0\u00a0\u00a0\u00a0 'India'),\r\n('John',\u00a0\u00a0 54,\u00a0 'Mumbai',\u00a0\u00a0\u00a0 'India'),\r\n(\"Michael\",21,\u00a0 'Tokyo',\u00a0\u00a0\u00a0\u00a0 'Japan')]\r\n\r\n#Creating an object of dataframe class\r\ndfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'Location' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f'])\r\n\r\n#Deleting 1st and 2nd row\r\ndfObj.drop([dfObj.index[0] , dfObj.index[1]],inplace=True)\r\nprint(dfObj)<\/pre>\n
Output :\r\n \u00a0 \u00a0 Name\u00a0 \u00a0 \u00a0 Age\u00a0\u00a0\u00a0\u00a0\u00a0Location\u00a0 \u00a0 \u00a0Country\r\nc\u00a0 \u00a0 \u00a0Kirti\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 39\u00a0 \u00a0 \u00a0New York\u00a0\u00a0\u00a0\u00a0 USA\r\nd\u00a0 \u00a0 \u00a0Veena\u00a0 \u00a0 \u00a0 40\u00a0\u00a0\u00a0\u00a0 Delhi\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 India\r\ne\u00a0\u00a0\u00a0\u00a0 John\u00a0 \u00a0 \u00a0 \u00a0 \u00a054\u00a0\u00a0\u00a0 Mumbai\u00a0 \u00a0 \u00a0 \u00a0 India\r\nf\u00a0 \u00a0 \u00a0Michael\u00a0 \u00a0 \u00a021\u00a0\u00a0\u00a0\u00a0 Tokyo\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Japan<\/pre>\n
\n