Professional Documents
Culture Documents
Dataframe+wrkst+-2+ (+with+solutions) Add
Dataframe+wrkst+-2+ (+with+solutions) Add
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
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
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
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
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