You are on page 1of 5

DataFrame Practice Worksheet -3 (Delete columns)

Consider the following dataframe and answer the following questions:


icode item dp color up disc acode vname qual area
icode
A21 Frock 2016-01-23 Red 700 10 123 Godrej NaN West
B26 Cot 2015-09-23 Blue 5000 25 456 P&G NaN East
NaN
B35 Soft 2016-06-17 Green 800 10 123 Godrej NaN East
C80 Toy
Baby 2014-10-16 Red 100 7 345 Tata NaN North
A30 Socks
Baby 2015-09-20 orange 500 5 123 Godrej NaN west
Suit

Q1. Write a program to create the above mentioned dataframe using the
index values as 10,20,30,40 and 50.
import pandas as pd
import numpy as np
dict1={'icode':['a21','b26','b35','c80','a30'],
'item':['frock','cot','soft toy','baby socks','baby suit'],
'dp':['2016-01-23','2015-09-23','2016-06-17','2014-10-16','2015-09-20'],
'color':['red','blue','green','red','orange'],
'up':[700,5000,800,100,500],
'disc':[10,25,10,7,5],
'acode':['123','456','123','345','123'],
'vname':['Godrej','P&G','Godrej','Tata','Godrej'],
'qual':[np.nan,np.nan,np.nan,np.nan,np.nan],
'area':['west','east','east','north','west'],
}
item1=pd.DataFrame(dict1,index=[10,20,30,40,50])
print(item1)
Q2. Write code to delete column qual (using del).
>>> del item1['qual']
>>> item1

icode item dp color up disc acode vname area

10 a21 frock 2016-01-23 red 700 10 123 Godrej west

20 b26 cot 2015-09-23 blue 5000 25 456 P&G east

30 b35 soft toy 2016-06-17 green 800 10 123 Godrej east

40 c80 baby socks 2014-10-16 red 100 7 345 Tata north

50 a30 baby suit 2015-09-20 orange 500 5 123 Godrej west

Q3. Can u delete multiple columns using del?


Ans 3 No, we cannot
Q4. Write code to delete column acode (using pop).
>>> item1.pop('acode')
10 123
20 456
30 123
40 345
50 123
Name: acode, dtype: object
Q4. Write code to delete column vname (using drop).
>>> item1.drop(axis=1,labels=['vname'])
icode item dp color up disc area
10 a21 frock 2016-01-23 red 700 10 west
20 b26 cot 2015-09-23 blue 5000 25 east
30 b35 soft toy 2016-06-17 green 800 10 east
40 c80 baby socks 2014-10-16 red 100 7 north
50 a30 baby suit 2015-09-20 orange 500 5 west

OR
>>> item1.drop(columns='vname', inplace=True)
>>> item1
icode item dp color up disc area
10 a21 frock 2016-01-23 red 700 10 west
20 b26 cot 2015-09-23 blue 5000 25 east
30 b35 soft toy 2016-06-17 green 800 10 east
40 c80 baby socks 2014-10-16 red 100 7 north
50 a30 baby suit 2015-09-20 orange 500 5 west
Q5. What difference do we see in deleting with del and pop?
del method doesn’t display the contents of the column deleted
Whereas
pop() also deletes an existing column as well displays the contents of the
column deleted.
Q6. Does drop() delete the column permanently from a dataframe? If no,
then how can we do that?
No, it doesn’t until and unless we use ( inplace=True )
Q7. Write code to delete color and area columns from the dataframe item1
temporarily in following two ways:
(a) Specifying axis
>>> item1.drop(axis=1, labels=['color','area'])
icode item dp up disc
10 a21 frock 2016-01-23 700 10
20 b26 cot 2015-09-23 5000 25
30 b35 soft toy 2016-06-17 800 10
40 c80 baby socks 2014-10-16 100 7
50 a30 baby suit 2015-09-20 500 5

(b) Without specifying axis


>>> item1.drop(columns=['color','area'])
icode item dp up disc
10 a21 frock 2016-01-23 700 10
20 b26 cot 2015-09-23 5000 25
30 b35 soft toy 2016-06-17 800 10
40 c80 baby socks 2014-10-16 100 7
50 a30 baby suit 2015-09-20 500 5
Q8. Write code to delete color and area columns from the dataframe item
permanently.
>>> item1.drop(columns=['color','area'], inplace=True)
>>> item1
icode item dp up disc
10 a21 frock 2016-01-23 700 10
20 b26 cot 2015-09-23 5000 25
30 b35 soft toy 2016-06-17 800 10
40 c80 baby socks 2014-10-16 100 7
50 a30 baby suit 2015-09-20 500 5

You might also like