Creating Dataframes Questions and Answers (Informatics Practices class 12)

Solved Assignment Create Dataframe

Question Answers

To read the answer click on a specific link.
import pandas as pd
def df_SingleColumn():
    l = [33,55,66,39,45]
    df = pd.DataFrame(l)
    print(df)
df_SingleColumn()
  • Create a dataframe with the above data (Q-5) and display the sum of the given numbers:
      import pandas as pd
    def df_SingleColumn():
    l = [33,55,66,39,45]
    sum1=0
    df = pd.DataFrame(l)
    sum1=df.sum()
    print("Sum of given numbers:", sum1.to_string(index=False))
    df_SingleColumn()
  • What will be the output of following code:
    import pandas as pd
    def df_data():
    df1=pd.DataFrame([22,56,78])
    df2=pd.DataFrame([[11,43,67]])
    print(df1)
    print(df2)
    df_data()
    Answer:

        0
    0 22
    1 56
    2 78
    0 1 2
    0 11 43 67

    In this code two datafames created. In the first dataframe there is a single-column list is which creates a single column. Similarly in the second dataframe, multiple columns created using double square brackets.

    Watch the complete video lesson
  • Create a dataframe named booking with the following data:
     TCode  Name  Tickets  Amount
     T0001  Anuj Maheta  5  1355
     T0002  Sandeep Oza  2  1169
     T0003  Manvi Sharma  6  1988
    import pandas as pd
    def df_booking():
    data = [['T0001','Anuj Maheta',5,1355],
    ['T0002','Sandeep Oza',2,1169,],
    ['T0003','Manavi Sharma',6,1988]]
    booking=pd.DataFrame(data,columns=['Tcode','Name','Tickets','Amount'])
    print(booking.to_string(index=False))
    df_booking()

  • Create a dataframe furniture shown in the following table:
     Item  Material  Colour  Price
     Sofa  Wooden  Maroon  25000
     Dining Table  Plywood Yellow  20000
     Chair  Plastic  Red  1500
     Sofa  Stainless Steel  Silver  55000
     Chair  Wooden   Light Blue  2500
     Dining Table  Aluminum  Golden  65000

    a) Display the details of the chair and sofa.
    b) Display furniture details which price is more than 25000.
    c) Display the furniture details price under 10000.
    d) Display alternative rows. 
    Answer:
    Creating Dataframe:
    import pandas as pd
    def df_furniture():
    data = [['Sofa','Wooden','Maroon',25000],
    ['Dining Table','Plywood','Yellow',20000],
    ['Chair','Plastic', 'Red',1500],
    ['Sofa','Stainless Steel', 'Silver',55000],
    ['Chair','Wooden', 'Light Blue',2500],
    ['Dining Table','Aluminum', 'Golden',65000],]
    furniture=pd.DataFrame(data,columns=['Item','Material','Colour','Price'])
    print(furniture.to_string(index=False))
    df_furniture()
    a) print(furniture[furniture[‘Item’]==’Sofa’],”n”,furniture[furniture[‘Item’]==’Chair’]) 
    b) print(furniture[furniture[‘Price’]>25000])
    c) print(furniture[furniture[‘Price’]<10000])
    d) print(furniture.iloc[::2])
  • Create a dataframe using the 2D dictionary to store the following records:
     House  Activity1  Activity2  Activity3
     Blue House  98  85  88
     Red House  87  76  80
     Green House  59  67  91
     Yellow House  78  99  55

    Answer:

    import pandas as pd
    def df_CCA():
    data = {'House':['Blue House','Red House','Green House','Yellow House'],
    'Activity1':[98,87,59,78],
    'Activity2':[85,76,67,99],
    'Activity3':[88,80,91,55]}
    furniture=pd.DataFrame(data)
    print(furniture.to_string(index=False))
    df_CCA()

  • Create a dataframe using the 2d dictionary with values as dictionary objects which store data of CCA activities for two terms:
       Red  Green  Blue  Yellow
     Term1  200  350  400  380
     Term2  189  250  375  300
    import pandas as pd
    def df_CCA():
    r={'Term1':200,'Term2':189}
    g={'Term1':350,'Term2':250}
    b={'Term1':400,'Term2':400}
    y={'Term1':380,'Term2':300}
    c={'Red':r,'Green':g,'Blue':b,'Yellow':y}
    df=pd.DataFrame(c)
    print(df)
    df_CCA()

  • Create a dataframe using the following matrix by ndarray, assign row, and column labels like MS excel.
       A  B  C
     1  98  56  87
     2  32  76  65
     3  55  99  88
    import pandas as pd
    import numpy as np
    def df_ndArray():
    nda=np.array([[98,56,97],[32,76,65],[55,99,88]])
    df=pd.DataFrame(nda,columns=['A','B','C'],index=[1,2,3])
    print(df)
    df_ndArray()

Download the python code used in this post.

Comment Your Views

%d bloggers like this: