You are on page 1of 4

DataFrame Practice Worksheet -2 (Add columns)

Consider the following dataframe and answer the following questions:

Consider Item DatePurchase UnitPrice Discount


the
A21 Frock 2016-01-23 700 10
following
B26 Cot 2015-09-23 5000 25
dataframe
B35 Soft Toy 2016-06-17 800 10
and
C80 Baby Socks 2014-10-16 100 7
attempt
A30 Baby Suit 2015-09-20 500 5
the
questions
Q1. Write a program to create the mentioned dataframe using the index
given
values as 10,20,30,40 and 50.
below:Cod
e

import pandas as pd
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'],
'up':[700,5000,800,100,500],
'disc':[10,25,10,7,5]}
item1=pd.DataFrame(dict1,index=[10,20,30,40,50])
print(item1)

Q2. Write code to insert a column named “vname” to store the following
vendor names- Godrej, P&G, Godrej, Tata, Godrej ( corresponding to every
row)
Ans 2. >>> item1['vname']=pd.Series(['Godrej','P&G','Godrej','Tata','Godrej'],index=[10,20,30,40,50])
>>> item1
icode item dp up disc vname
10 a21 frock 2016-01-23 700 10 Godrej
20 b26 cot 2015-09-23 5000 25 P&G
30 b35 soft toy 2016-06-17 800 10 Godrej
40 c80 baby socks 2014-10-16 100 7 Tata
50 a30 baby suit 2015-09-20 500 5 Godrej
Note: In the above example if we do not mention the index values then the
column is though inserted but the values in it are put an NaN. To check that
lets try the next question.
Q3. Write code to insert a column named “qual” to store the quality
specifications which are like a+, a, b, a+, a for every row.
>>> item1['qual']=pd.Series(['a+','a','b','a+','a'])
>>> item1

icode item dp up disc vname qual

10 a21 frock 2016-01-23 700 10 Godrej NaN

20 b26 cot 2015-09-23 5000 25 P&G NaN

30 b35 soft toy 2016-06-17 800 10 Godrej NaN

40 c80 baby socks 2014-10-16 100 7 Tata NaN

50 a30 baby suit 2015-09-20 500 5 Godrej NaN

Note: So by the use of Series method to insert a column, if we do not mention the indexes,
then the column is inserted but values are not. Instead all values in that column become
NaN.

Q4. Write code to insert a column named “area” to store the following values
– west, east, east north and west for each row with the help of assign
function.
>>> item1.assign(area=['west','east','east','north','west'])
icode item dp up disc vname qual area

10 a21 frock 2016-01-23 700 10 Godrej NaN west

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

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

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

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


Note : Use of assign() does not need index values to insert a column with all its values
BUT
In this case if do not assign the output of the assign function back into our dataframe then
the addition of the new column is not reflected in the dataframe permanently. This we can
confirm by checking the contents of item1 now.
>>> item1

icode item dp up disc vname qual

10 a21 frock 2016-01-23 700 10 Godrej NaN

20 b26 cot 2015-09-23 5000 25 P&G NaN

30 b35 soft toy 2016-06-17 800 10 Godrej NaN

40 c80 baby socks 2014-10-16 100 7 Tata NaN

50 a30 baby suit 2015-09-20 500 5 Godrej NaN

NOW: if we assign the output back into the dataframe item1 then my column gets
permanently added.
>>> item1=item1.assign(area=['west','east','east','north','west'])

>>> item1

icode item dp up disc vname qual area

10 a21 frock 2016-01-23 700 10 Godrej NaN west

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

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

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

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


Q5. Write a command to insert a column named “acode” with the following
values ['123','456','123','345','123'] at the 6th column position ( column index
=5)
>>> acode=['123','456','123','345','123']
>>> item1.insert(loc=5,column='acode',value=acode)
>>> item1

icode item dp up disc acode vname qual area

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

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

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

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

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

Q6. Write a command to insert a column named “color” with the following
values ['red','blue','green','red','orange'] at the 4th column position ( column
index =3)
>>> item1.insert(loc=3,column='color',value=['red','blue','green','red','orange'])
>>> item1

icode item dp color up disc acode vname qual area

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

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

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

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

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

You might also like