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 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.
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.
Removing a Column from a Python Dataframe
There are 3 simple methods to do this task.They are:
- Using pop() method
- Using del Keyword
- Using drop() method
1)Using pop() method
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.
Syntax:
pandas.dataframe.pop('ColumnName')
For Example:
Approach:
- Import pandas module using the import keyword.
- Give some random list of data(as dictionary) and store it in a variable.
- Pass the given data to the DataFrame() function and store it in another variable.
- Print the above result.
- Remove some random column(salary) from the given dataframe using the pop() method by passing the column name as an argument.
- Print the above data after removing the specified column(salary).
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword import pandas as pd # Give some random list of data and store it in a variable gvn_data = {"ID": [11, 12, 13, 14, 15, 16], "Name": ["peter", "irfan", "mary", "riya", "virat", "sunny"], "salary": [10000, 25000, 15000, 50000, 30000, 22000]} # Pass the given data to the DataFrame() function and store it in another variable block_data = pd.DataFrame(gvn_data) # Print the above result print("The given input Dataframe: ") print(block_data) print() # Remove some random column(salary) from the given dataframe using the pop() method # by passing the columnname as an argument. block_data.pop('salary') # Print the above data after removing the specified column(salary) print("The given data after removing the 'salary' column: ") print(block_data)
Output:
The given input Dataframe: ID Name salary 0 11 peter 10000 1 12 irfan 25000 2 13 mary 15000 3 14 riya 50000 4 15 virat 30000 5 16 sunny 22000 The given data after removing the 'salary' column: ID Name 0 11 peter 1 12 irfan 2 13 mary 3 14 riya 4 15 virat 5 16 sunny
2)Using del Keyword
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.
Syntax:
del dataframe['ColumnName']
Approach:
- Import pandas module using the import keyword.
- Give some random list of data(as dictionary) and store it in a variable.
- Pass the given data to the DataFrame() function and store it in another variable.
- Print the above result.
-
Delete some random column(Name) from the given dataframe using the del keyword.
- Print the above data after removing the specified column(Name).
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword import pandas as pd # Give some random list of data and store it in a variable gvn_data = {"ID": [11, 12, 13, 14, 15, 16], "Name": ["peter", "irfan", "mary", "riya", "virat", "sunny"], "salary": [10000, 25000, 15000, 50000, 30000, 22000]} # Pass the given data to the DataFrame() function and store it in another variable block_data = pd.DataFrame(gvn_data) # Print the above result print("The given input Dataframe: ") print(block_data) print() # Delete some random column(Name) from the given dataframe using the del keyword del block_data['Name'] # Print the above data after removing the specified column(Name) print("The given data after removing the 'Name' column: ") print(block_data)
Output:
The given input Dataframe: ID Name salary 0 11 peter 10000 1 12 irfan 25000 2 13 mary 15000 3 14 riya 50000 4 15 virat 30000 5 16 sunny 22000 The given data after removing the 'Name' column: ID salary 0 11 10000 1 12 25000 2 13 15000 3 14 50000 4 15 30000 5 16 22000
3)Using drop() Method
We can remove values from a data frame using the pandas.dataframe.drop() function. The values can be either row- or column-oriented.
Syntax:
dataframe.drop('ColumnName', inplace=True, axis=1)
ColumnName: The column that you want to remove.
inplace: If set inplace=True, the changes are stored in a new object that is created without affecting the original dataframe.
axis: axis= 1 represents column-wise operations and 0 represents row-wise operations.
For Example:
Approach:
- Import pandas module using the import keyword.
- Give some random list of data(as dictionary) and store it in a variable.
- Pass the given data to the DataFrame() function and store it in another variable.
- Print the above result.
-
Remove some random column(salary) from the given dataframe using the drop() method by passing the column-name, inplace=True, axis=1 as the arguments to it.
- Print the above data after removing the specified column(salary).
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword import pandas as pd # Give some random list of data(as dictionary) and store it in a variable gvn_data = {"ID": [11, 12, 13, 14, 15, 16], "Name": ["peter", "irfan", "mary", "riya", "virat", "sunny"], "salary": [10000, 25000, 15000, 50000, 30000, 22000]} # Pass the given data to the DataFrame() function and store it in another variable block_data = pd.DataFrame(gvn_data) # Print the above result print("The given input Dataframe: ") print(block_data) print() # Remove some random column(salary) from the given dataframe using the drop() method # by passing the columnname, inplace=True, axis=1 as the arguments to it. block_data.drop('salary', inplace=True, axis=1) # Print the above data after removing the specified column(salary) print("The given data after removing the 'salary' column: ") print(block_data)
Output:
The given input Dataframe: ID Name salary 0 11 peter 10000 1 12 irfan 25000 2 13 mary 15000 3 14 riya 50000 4 15 virat 30000 5 16 sunny 22000 The given data after removing the 'salary' column: ID Name 0 11 peter 1 12 irfan 2 13 mary 3 14 riya 4 15 virat 5 16 sunny