How to Delete Column of Pandas DataFrame?

How to Delete Column of Pandas DataFrame?

Pandas DataFrame ¨C Delete Column

There are different ways to delete a column of Pandas DataFrame. Following are some of the ways we will

discuss in this tutorial.

Delete column using del keyword.

Delete column using drop() method.

Delete column using pop() method.

Delete DataFrame Column using del keyword

To delete a column of DataFrame using del keyword, use the following syntax.

del

myDataFrame['column_

name']

del myDataFrame['column_name']

In the following example, we shall initialize a DataFrame with three columns and delete one of the column using

del keyword.

Python Program

import pandas as pd

#initialize a dataframe

df = pd.DataFrame({

'a':[14,pandas

52, 46],

import

as pd

'b':[32, 85, 64],

'c':[88, 47, 36]})

#initialize a dataframe

#delete column 'b'

df =df['b']

pd.DataFrame({

del

'a':[14, 52, 46],

#print the dataframe

print(df)

'b':[32, 85, 64],

'c':[88, 47, 36]})

#delete column 'b'

del df['b']

#print the dataframe

print(df)

Output

a c

0 14 88

1 52 47

2 46 36

ac

0 14 88

1 52 47

2 46 36

The column with name b has been deleted from the dataframe.

Delete DataFrame Column using drop() method

pandas.DataFrame.drop() method returns a new DataFrame with the specified columns dropped from the

original DataFrame. The original DataFrame is not modified.

Python Program

import pandas as pd

#initialize a dataframe

df = pd.DataFrame({

'a':[14,pandas

52, 46],

import

as pd

'b':[32, 85, 64],

'c':[88, 47, 36]})

#initialize a dataframe

#delete column 'b'

df ==pd.DataFrame({

df1

df.drop(['b'],

axis=1)

'a':[14, 52, 46],

#print

the dataframe

'b':[32,

85, 64],

print(df1)

'c':[88, 47, 36]})

#delete column 'b'

df1 = df.drop(['b'], axis=1)

#print the dataframe

print(df1)

Output

a c

0 14 88

1 52 47

2 46 36

ac

0 14 88

1 52 47

2 46 36

Delete DataFrame Column using pop() method

pandas.DataFrame.pop() method deletes specified column from the DataFrame and returns the deleted

column.

Python Program

import pandas as pd

#initialize a dataframe

df = pd.DataFrame({

'a':[14,pandas

52, 46],

import

as pd

'b':[32, 85, 64],

'c':[88, 47, 36]})

#initialize a dataframe

#delete column 'b'

df = pd.DataFrame({

poppedColumn

=

df.pop('b')

'a':[14, 52, 46],

#print

the dataframe

'b':[32,

85, 64],

print(df)

'c':[88, 47, 36]})

print('\nDeleted

Column\n------------')#delete column 'b'

#print

deleted column

poppedColumn

= df.pop('b')

print(poppedColumn)

#print the dataframe

print(df)

print('\nDeleted Column\n-------------')

#print deleted column

print(poppedColumn)

Output

a c

0 14 88

1 52 47

2 46 36

ac

Deleted Column

0 14 88

------------01 523247

1 85

22 466436

Name: b, dtype: int64

Deleted Column

------------0 32

1 85

2 64

Name: b, dtype: int64

Conclusion

In this Pandas Tutorial, we learned how to delete a column from DataFrame.

Python Pandas

?

Pandas Tutorial

?

Pandas DataFrame - Change Column Names

?

Pandas DataFrame - Add or Append Row

?

Pandas DataFrame - Add new column

?

Pandas DataFrame - Delete Column

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches