{"id":5609,"date":"2023-10-27T09:30:43","date_gmt":"2023-10-27T04:00:43","guid":{"rendered":"https:\/\/python-programs.com\/?p=5609"},"modified":"2023-11-10T12:02:27","modified_gmt":"2023-11-10T06:32:27","slug":"python-pandas-drop-columns-in-dataframe-by-label-names-or-by-index-positions","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-pandas-drop-columns-in-dataframe-by-label-names-or-by-index-positions\/","title":{"rendered":"Python Pandas : Drop columns in DataFrame by label Names or by Index Positions"},"content":{"rendered":"
In this article, we are going to demonstrate how to drop columns in a dataframe by their labels or index. So, let’s start exploring the topic in detail.<\/p>\n
In dataframe there is a function Syntax – DataFrame.drop<\/em><\/p>\n Where,<\/p>\n We will be using the following dataset as example :<\/p>\n We can delete a single column just by passing its name into the function. Let\u2019s try deleting \u2018 Let’s see the program how to implement this.<\/p>\n To delete multiple columns by name we just have to pass all the names as a list into the function. Let\u2019s try deleting \u2018Age\u2019 and \u2018Exp\u2019<\/p>\n Let’s see the program how to implement this.<\/p>\n In case we know the index position of the columns we want to drop, we can pass them into the function. Let\u2019s try deleting the same two columns as above but with their index position.<\/p>\n Let’s see the program how to implement this.<\/p>\n In case we don’t want a new dataframe object to hold the modified values, but want to store it in the same object, we can do it by passing Let’s see the program how to implement this.<\/p>\n In case the column\/row does not exist we can do a check beforehand to avoid further bugs in the program. We can do so by using the same 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 columns in DataFrame by label Names or by Index Positions in Python ? In this article, we are going to demonstrate how to drop columns in a dataframe by their labels or index. So, let’s start exploring the topic in detail. In dataframe there is a function drop() which can be used …<\/p>\ndrop()<\/code> which can be used to drop columns.<\/p>\n
(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')<\/pre>\n
\n
True<\/code> it deletes the columns, else the rows<\/li>\n
False<\/code> then it doesn\u2019t modify the dataframe, it returns a new one. If it is set to
True<\/code> it modifies the dataframe.<\/li>\n<\/ul>\n
\u00a0 Regd\u00a0\u00a0\u00a0\u00a0 Name\u00a0\u00a0 Age\u00a0\u00a0\u00a0\u00a0\u00a0 City\u00a0 Exp\r\na\u00a0\u00a0\u00a0 10\u00a0\u00a0\u00a0\u00a0 Jill\u00a0 16.0\u00a0\u00a0\u00a0\u00a0 Tokyo\u00a0\u00a0 10\r\nb\u00a0\u00a0\u00a0 11\u00a0\u00a0 Rachel\u00a0 38.0\u00a0\u00a0\u00a0\u00a0 Texas\u00a0\u00a0\u00a0 5\r\nc\u00a0\u00a0\u00a0 12\u00a0\u00a0\u00a0 Kirti\u00a0 39.0\u00a0 New York\u00a0\u00a0\u00a0 7\r\nd\u00a0\u00a0\u00a0 13\u00a0\u00a0\u00a0 Veena\u00a0 40.0\u00a0\u00a0\u00a0\u00a0 Texas\u00a0\u00a0 21\r\ne\u00a0\u00a0\u00a0 14\u00a0 Lucifer\u00a0\u00a0 NaN\u00a0\u00a0\u00a0\u00a0 Texas\u00a0\u00a0 30\r\nf\u00a0\u00a0\u00a0 15\u00a0\u00a0\u00a0 Pablo\u00a0 30.0\u00a0 New York\u00a0\u00a0\u00a0 7\r\ng\u00a0\u00a0\u00a0 16\u00a0\u00a0 Lionel\u00a0 45.0\u00a0 Colombia\u00a0\u00a0 11<\/pre>\n
Delete a Single column in DataFrame by Column Name :<\/h3>\n
Age<\/code>\u2019 column from the Dataframe.<\/p>\n
#Program :\r\n\r\nimport numpy as np\r\nimport pandas as pd\r\n\r\n# Example data\r\nstudents = [(10,'Jill', 16, 'Tokyo', 10),\r\n (11,'Rachel', 38, 'Texas', 5),\r\n (12,'Kirti', 39, 'New York', 7),\r\n (13,'Veena', 40, 'Texas', 21),\r\n (14,'Lucifer', np.NaN, 'Texas', 30),\r\n (15,'Pablo', 30, 'New York', 7),\r\n (16,'Lionel', 45, 'Colombia', 11) ]\r\n#Creating a dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Regd','Name','Age','City','Exp'], index=['a', 'b', 'c' , 'd' , 'e' , 'f', 'g']) \r\n#Modifying the dataframe and storing it into a new object\r\nmodDfObj = dfObj.drop('Age' , axis='columns')\r\nprint(modDfObj)\r\n<\/pre>\n
Output :\r\n \u00a0 Regd\u00a0\u00a0\u00a0\u00a0 Name\u00a0\u00a0\u00a0\u00a0\u00a0 City\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Exp\r\na\u00a0\u00a0\u00a0 10\u00a0 \u00a0 \u00a0 \u00a0 Jill\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Tokyo\u00a0 \u00a0 \u00a0 10\r\nb\u00a0\u00a0\u00a0 11\u00a0 \u00a0 \u00a0 Rachel\u00a0\u00a0\u00a0\u00a0 Texas\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 5\r\nc\u00a0\u00a0\u00a0 12\u00a0 \u00a0 \u00a0 \u00a0Kirti\u00a0 \u00a0 \u00a0 \u00a0New York\u00a0\u00a0\u00a0 7\r\nd\u00a0\u00a0\u00a0 13\u00a0 \u00a0 \u00a0 Veena\u00a0\u00a0\u00a0\u00a0 Texas\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 21\r\ne\u00a0\u00a0\u00a0 14\u00a0 \u00a0 \u00a0 Lucifer\u00a0\u00a0\u00a0\u00a0 Texas\u00a0 \u00a0 \u00a0 \u00a0 \u00a030\r\nf\u00a0\u00a0\u00a0 15\u00a0 \u00a0 \u00a0 \u00a0Pablo\u00a0 \u00a0 \u00a0New York\u00a0\u00a0\u00a0 7\r\ng\u00a0\u00a0\u00a0 16\u00a0 \u00a0 \u00a0 Lionel\u00a0 \u00a0 Colombia\u00a0 \u00a0 11<\/pre>\n
Drop Multiple Columns by Label Names in DataFrame :<\/h3>\n
#Program :\r\n\r\nimport numpy as np\r\nimport pandas as pd\r\n\r\n# Example data\r\nstudents = [(10,'Jill', 16, 'Tokyo', 10),\r\n (11,'Rachel', 38, 'Texas', 5),\r\n (12,'Kirti', 39, 'New York', 7),\r\n (13,'Veena', 40, 'Texas', 21),\r\n (14,'Lucifer', np.NaN, 'Texas', 30),\r\n (15,'Pablo', 30, 'New York', 7),\r\n (16,'Lionel', 45, 'Colombia', 11) ]\r\n#Creating a dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Regd','Name','Age','City','Exp'], index=['a', 'b', 'c' , 'd' , 'e' , 'f', 'g']) \r\n#Modifying the dataframe without the columns and storing it into a new object\r\nmodDfObj = dfObj.drop(['Age' , 'Exp'] , axis='columns')\r\nprint(modDfObj)\r\n<\/pre>\n
Output :\r\n Regd\u00a0\u00a0\u00a0Name\u00a0\u00a0\u00a0\u00a0\u00a0City\r\na\u00a0\u00a0\u00a0 10\u00a0\u00a0\u00a0\u00a0 Jill\u00a0\u00a0\u00a0\u00a0 Tokyo\r\nb\u00a0\u00a0\u00a0 11\u00a0\u00a0 Rachel\u00a0\u00a0\u00a0\u00a0 Texas\r\nc\u00a0\u00a0\u00a0 12\u00a0\u00a0\u00a0 Kirti\u00a0 New York\r\nd\u00a0\u00a0\u00a0 13\u00a0\u00a0\u00a0 Veena\u00a0\u00a0\u00a0\u00a0 Texas\r\ne\u00a0\u00a0\u00a0 14\u00a0 Lucifer\u00a0\u00a0\u00a0\u00a0 Texas\r\nf\u00a0\u00a0\u00a0 15\u00a0\u00a0\u00a0 Pablo\u00a0 New York\r\ng\u00a0\u00a0\u00a0 16\u00a0\u00a0 Lionel\u00a0 Colombia<\/pre>\n
Drop Columns by Index Position in DataFrame :<\/h3>\n
#Program :\r\n\r\nimport numpy as np\r\nimport pandas as pd\r\n\r\n# Example data\r\nstudents = [(10,'Jill', 16, 'Tokyo', 10),\r\n (11,'Rachel', 38, 'Texas', 5),\r\n (12,'Kirti', 39, 'New York', 7),\r\n (13,'Veena', 40, 'Texas', 21),\r\n (14,'Lucifer', np.NaN, 'Texas', 30),\r\n (15,'Pablo', 30, 'New York', 7),\r\n (16,'Lionel', 45, 'Colombia', 11) ]\r\n#Creating a dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Regd','Name','Age','City','Exp'], index=['a', 'b', 'c' , 'd' , 'e' , 'f', 'g']) \r\n#Modifying the dataframe without the columns and storing it into a new object by passsing the index of the columns\r\nmodDfObj = dfObj.drop([dfObj.columns[2] , dfObj.columns[4]] , axis='columns')\r\nprint(modDfObj)\r\n<\/pre>\n
Output :\r\n \u00a0 Regd\u00a0\u00a0\u00a0Name\u00a0\u00a0\u00a0\u00a0\u00a0 City\r\na\u00a0\u00a0\u00a0 10\u00a0\u00a0\u00a0\u00a0 Jill\u00a0\u00a0\u00a0\u00a0 Tokyo\r\nb\u00a0\u00a0\u00a0 11\u00a0\u00a0 Rachel\u00a0\u00a0\u00a0\u00a0 Texas\r\nc\u00a0\u00a0\u00a0 12\u00a0\u00a0\u00a0 Kirti\u00a0 New York\r\nd\u00a0\u00a0\u00a0 13\u00a0\u00a0\u00a0 Veena\u00a0\u00a0\u00a0\u00a0 Texas\r\ne\u00a0\u00a0\u00a0 14\u00a0 Lucifer\u00a0\u00a0\u00a0\u00a0 Texas\r\nf\u00a0\u00a0\u00a0 15\u00a0\u00a0\u00a0 Pablo\u00a0 New York\r\ng\u00a0\u00a0\u00a0 16\u00a0\u00a0 Lionel\u00a0 Colombia<\/pre>\n
Drop Columns in Place :<\/h3>\n
inplace= True<\/code>. <\/em>Let\u2019s use the previous example for this.<\/p>\n
#Program :\r\n\r\nimport numpy as np\r\nimport pandas as pd\r\n\r\n# Example data\r\nstudents = [(10,'Jill', 16, 'Tokyo', 10),\r\n (11,'Rachel', 38, 'Texas', 5),\r\n (12,'Kirti', 39, 'New York', 7),\r\n (13,'Veena', 40, 'Texas', 21),\r\n (14,'Lucifer', np.NaN, 'Texas', 30),\r\n (15,'Pablo', 30, 'New York', 7),\r\n (16,'Lionel', 45, 'Colombia', 11) ]\r\n#Creating a dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Regd','Name','Age','City','Exp'], index=['a', 'b', 'c' , 'd' , 'e' , 'f', 'g']) \r\n#Modifying the dataframe without the columns and storing it into the same object \r\ndfObj.drop([dfObj.columns[2] , dfObj.columns[4]] , axis='columns',inplace = True)\r\nprint(dfObj)\r\n<\/pre>\n
Output :\r\n \u00a0 \u00a0Regd\u00a0\u00a0\u00a0\u00a0 Name\u00a0\u00a0\u00a0\u00a0\u00a0 City\r\na\u00a0\u00a0\u00a0 10\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Jill\u00a0 \u00a0 \u00a0 \u00a0 \u00a0Tokyo\r\nb\u00a0\u00a0\u00a0 11\u00a0 \u00a0 \u00a0 \u00a0Rachel\u00a0\u00a0\u00a0\u00a0 Texas\r\nc\u00a0\u00a0\u00a0 12\u00a0 \u00a0 \u00a0 \u00a0 Kirti\u00a0 \u00a0 \u00a0 \u00a0New York\r\nd\u00a0\u00a0\u00a0 13\u00a0 \u00a0 \u00a0 \u00a0Veena\u00a0\u00a0\u00a0\u00a0 Texas\r\ne\u00a0\u00a0\u00a0 14\u00a0 \u00a0 \u00a0 Lucifer\u00a0\u00a0\u00a0\u00a0 Texas\r\nf\u00a0\u00a0\u00a0 15\u00a0 \u00a0 \u00a0 \u00a0Pablo\u00a0 \u00a0 New York\r\ng\u00a0\u00a0\u00a0 16\u00a0 \u00a0 \u00a0Lionel\u00a0 \u00a0 \u00a0Colombia<\/pre>\n
Drop Column If Exists :<\/h3>\n
drop( )<\/code> , it checks for the columns and if it is not found it returns
KeyError<\/code> which we can handle by an if-else condition.<\/p>\n
#program :\r\n\r\nimport numpy as np\r\nimport pandas as pd\r\n\r\n# Example data\r\nstudents = [(10,'Jill', 16, 'Tokyo', 10),\r\n (11,'Rachel', 38, 'Texas', 5),\r\n (12,'Kirti', 39, 'New York', 7),\r\n (13,'Veena', 40, 'Texas', 21),\r\n (14,'Lucifer', np.NaN, 'Texas', 30),\r\n (15,'Pablo', 30, 'New York', 7),\r\n (16,'Lionel', 45, 'Colombia', 11) ]\r\n#Creating a dataframe object\r\ndfObj = pd.DataFrame(students, columns=['Regd','Name','Age','City','Exp'], index=['a', 'b', 'c' , 'd' , 'e' , 'f', 'g']) \r\n#Checking for a non-existent column\r\nif 'Last Name' in dfObj.columns :\r\n dfObj.drop('Last Name' , axis='columns')\r\n print(dfObj)\r\nelse :\r\n print(\"The column was not found\")\r\n<\/pre>\n
Output :\r\nThe column was not found<\/pre>\n
\n