{"id":26219,"date":"2022-01-03T09:19:58","date_gmt":"2022-01-03T03:49:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=26219"},"modified":"2022-01-03T09:19:58","modified_gmt":"2022-01-03T03:49:58","slug":"methods-for-removing-a-column-from-a-python-dataframe","status":"publish","type":"post","link":"https:\/\/python-programs.com\/methods-for-removing-a-column-from-a-python-dataframe\/","title":{"rendered":"Methods for Removing a Column from a Python Dataframe"},"content":{"rendered":"
Python provides us with a number of data structures through which we can interact with data and perform operations on it. Particularly when it comes to data science and analysis, the data structures provided by Python have given shape to the processing it.<\/p>\n
Dataframe:<\/strong><\/p>\n Python provides one such data structure, DataFrame. It saves data in the form of rows and columns. The datasets can be analyzed within the environment. These synchronized rows and columns are ready for data preprocessing and manipulation.<\/p>\n The Python Pandas module provides a data structure called a DataFrame. It organizes data into rows and columns and stores it. As a result, we can have the data in the form of a matrix, with the entities represented as rows and columns.<\/p>\n There are 3 simple methods to do this task.They are:<\/p>\n The pandas.dataframe.pop() method is used to remove or delete a column from a data frame by simply passing the column name as an argument.<\/p>\n Syntax:<\/strong><\/p>\n For Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The Python del keyword can also be used to remove a column from a data frame. In Python, the del keyword is generally used to delete or flush out objects.<\/p>\n Syntax:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can remove values from a data frame using the pandas.dataframe.drop() function. The values can be either row- or column-oriented.<\/p>\n Syntax:<\/strong><\/p>\n ColumnName<\/strong>: The column that you want to remove.<\/p>\n inplace:<\/strong> If set inplace=True, the changes are stored in a new object that is created without affecting the original dataframe. For Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Python provides us with a number of data structures through which we can interact with data and perform operations on it. Particularly when it comes to data science and analysis, the data structures provided by Python have given shape to the processing it. Dataframe: Python provides one such data structure, DataFrame. It saves data in …<\/p>\nRemoving a Column from a Python Dataframe<\/h2>\n
\n
1)Using pop() method<\/h5>\n
pandas.dataframe.pop('ColumnName')<\/pre>\n
\n
# Import pandas module using the import keyword\r\nimport pandas as pd\r\n# Give some random list of data and store it in a variable\r\ngvn_data = {\"ID\": [11, 12, 13, 14, 15, 16], \"Name\": [\"peter\", \"irfan\", \"mary\",\r\n \"riya\", \"virat\", \"sunny\"], \"salary\": [10000, 25000, 15000, 50000, 30000, 22000]}\r\n# Pass the given data to the DataFrame() function and store it in another variable\r\nblock_data = pd.DataFrame(gvn_data)\r\n# Print the above result\r\nprint(\"The given input Dataframe: \")\r\nprint(block_data)\r\nprint()\r\n# Remove some random column(salary) from the given dataframe using the pop() method\r\n# by passing the columnname as an argument.\r\nblock_data.pop('salary')\r\n# Print the above data after removing the specified column(salary)\r\nprint(\"The given data after removing the 'salary' column: \")\r\nprint(block_data)\r\n<\/pre>\n
The given input Dataframe: \r\n ID Name salary\r\n0 11 peter 10000\r\n1 12 irfan 25000\r\n2 13 mary 15000\r\n3 14 riya 50000\r\n4 15 virat 30000\r\n5 16 sunny 22000\r\n\r\nThe given data after removing the 'salary' column: \r\n ID Name\r\n0 11 peter\r\n1 12 irfan\r\n2 13 mary\r\n3 14 riya\r\n4 15 virat\r\n5 16 sunny<\/pre>\n
2)Using del Keyword<\/h5>\n
del dataframe['ColumnName']<\/pre>\n
\n
# Import pandas module using the import keyword\r\nimport pandas as pd\r\n# Give some random list of data and store it in a variable\r\ngvn_data = {\"ID\": [11, 12, 13, 14, 15, 16], \"Name\": [\"peter\", \"irfan\", \"mary\",\r\n \"riya\", \"virat\", \"sunny\"], \"salary\": [10000, 25000, 15000, 50000, 30000, 22000]}\r\n# Pass the given data to the DataFrame() function and store it in another variable\r\nblock_data = pd.DataFrame(gvn_data)\r\n# Print the above result\r\nprint(\"The given input Dataframe: \")\r\nprint(block_data)\r\nprint()\r\n# Delete some random column(Name) from the given dataframe using the del keyword\r\ndel block_data['Name']\r\n# Print the above data after removing the specified column(Name)\r\nprint(\"The given data after removing the 'Name' column: \")\r\nprint(block_data)\r\n<\/pre>\n
The given input Dataframe: \r\n ID Name salary\r\n0 11 peter 10000\r\n1 12 irfan 25000\r\n2 13 mary 15000\r\n3 14 riya 50000\r\n4 15 virat 30000\r\n5 16 sunny 22000\r\n\r\nThe given data after removing the 'Name' column: \r\n ID salary\r\n0 11 10000\r\n1 12 25000\r\n2 13 15000\r\n3 14 50000\r\n4 15 30000\r\n5 16 22000<\/pre>\n
3)Using drop() Method<\/h5>\n
dataframe.drop('ColumnName', inplace=True, axis=1)<\/pre>\n
\naxis<\/strong>: axis= 1 represents column-wise operations and 0 represents row-wise operations.<\/p>\n\n
# Import pandas module using the import keyword\r\nimport pandas as pd\r\n# Give some random list of data(as dictionary) and store it in a variable\r\ngvn_data = {\"ID\": [11, 12, 13, 14, 15, 16], \"Name\": [\"peter\", \"irfan\", \"mary\",\r\n \"riya\", \"virat\", \"sunny\"], \"salary\": [10000, 25000, 15000, 50000, 30000, 22000]}\r\n# Pass the given data to the DataFrame() function and store it in another variable\r\nblock_data = pd.DataFrame(gvn_data)\r\n# Print the above result\r\nprint(\"The given input Dataframe: \")\r\nprint(block_data)\r\nprint()\r\n# Remove some random column(salary) from the given dataframe using the drop() method\r\n# by passing the columnname, inplace=True, axis=1 as the arguments to it.\r\nblock_data.drop('salary', inplace=True, axis=1)\r\n# Print the above data after removing the specified column(salary)\r\nprint(\"The given data after removing the 'salary' column: \")\r\nprint(block_data)<\/pre>\n
The given input Dataframe: \r\n ID Name salary\r\n0 11 peter 10000\r\n1 12 irfan 25000\r\n2 13 mary 15000\r\n3 14 riya 50000\r\n4 15 virat 30000\r\n5 16 sunny 22000\r\n\r\nThe given data after removing the 'salary' column: \r\n ID Name\r\n0 11 peter\r\n1 12 irfan\r\n2 13 mary\r\n3 14 riya\r\n4 15 virat\r\n5 16 sunny<\/pre>\n