# Write a program in Python to remove one or more than one columns in a given DataFrame

Assume, you have a dataframe,

 one  two three
0 1    2    3
1 4    5    6

And the result for removing single column is,

 two three
0 2    3
1 5    6

The result for removing after more than one column is,

 three
0 3
1 6

To solve this, we will follow the steps given below −

## Solution 1

• Define a dataframe

• Delete a particular column using below method,

del df['one']

### Example

Let’s see the below code to get a better understanding −

import pandas as pd
data = [[1,2,3],[4,5,6]]
df = pd.DataFrame(data,columns=('one','two','three'))
print("Before deletion\n", df)
del df['one']
print("After deletion\n", df)

### Output

Before deletion
one two three
0 1    2    3
1 4    5    6
After deletion
two three
0 2    3
1 5    6

## Solution 2

• Define a dataframe

• Delete a particular column using pop function. It is defined below

df.pop('one')

### Example

import pandas as pd
data = [[1,2,3],[4,5,6]]
df = pd.DataFrame(data,columns=('one','two','three'))
print("Before deletion\n", df)
df.pop('one')
print("After deletion\n", df)

### Output

Before deletion
one two three
0 1    2    3
1 4    5    6
After deletion
two three
0 2    3
1 5    6

## Solution 3

• Define a dataframe

• Apply the below method to drop more than one columns,

df.drop(columns=['one','two'],inplace = True)

### Example

import pandas as pd
data = [[1,2,3],[4,5,6]]
df = pd.DataFrame(data,columns=('one','two','three'))
print("Before deletion\n ", df)
df.drop(columns=['one','two'],inplace = True)
print("After deleting two columns\n", df)

### Output

Before deletion
one two three
0 1    2    3
1 4    5    6
After deletion
two three
0 2    3
1 5    6
After deleting two columns
three
0 3
1 6

Updated on: 24-Feb-2021

118 Views