CBSE CS and IP

CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

DataFrame operations (2 Marks Questions)

  1. What will be output after following program execution?
    import pandas as pd
    table = {
     "Name": ["anil", "vishal","manish","mohak"],
     "Age": [12,34,22,14],
     }
    df = pd.DataFrame(table)
    print(df)
    print (df.pivot_table(index="Name",columns="Name",values="Age"))

  2. Explain the following groupby() attributes with examples:

    • size
    • count

  3. Explain the agg() function on groupby object created using groupby() method?

  4. What is the use of transform() function? Explain with the help of example?

  5. A dataframe df1 stores data about passengers, flight and years as below:


  6. Year
    Month
    Passengers
    0
    1
    2
    3
    4
    2009
    2009
    2009
    2009
    2009
    January
    February
    March
    March
    May
    112
    118
    132
    129
    121

    • Compute total passenger per year
    • Compute average passengers per month.

  7. For the given code fill in the blanks so that we get the desired output with sorting the dataframe first on Quantity and second on Cost.
    import pandas as pd
    import numpy as np
    d = {'Product':['Apple','Pear','Banana','Grapes'],
        'Quantity':[100,100,200,250],
        'Cost':[1000,1500,1200,900]}
    df = pd.DataFrame(d)
    df1 = ______________________________________
    print(df1)
     Product Quantity Cost
    0 Apple 100   1000
    1 Pear 100   1500
    2 Banana 200   1200
    3 Grapes 250   900
    
  8. Consider the following dataframes :
    df1                               df2
         mark1   mark2                mark1    mark2
    0    10        15                0     30          20
    1    40        45                1     20          25
    2    15        30                2     20          30
    3    40        70                3     50          30
    
    Write the commands to do the following operations on the dataframes given above :
    • To rename column mark1 as Score1 in both the dataframes df1 and df2.
    • To change index label of df1 from 0 to zero and from 1 to one.
  9. What is Pivoting? Name any two functions of Pandas which support pivoting.
  10. What is difference between pivot() and pivot_table() functions?
  11. What do you understand by reshaping of a data frame. Also, name the methods which are used to do so?
  12. How to Check if a Data Frame has any missing values ?

No comments:

Post a Comment