Page 96 - Informatics_Practices_Fliipbook_Class12
P. 96

23.  Sushila has created a DataFrame with the help of the following code:                        [2022]
              import pandas
              EMP={'EMPID': ['E01', 'E02', 'E03', 'E04', 'E05'],
                     'EMPNAME' : ['KISHORI','PRIYA', 'DAMODAR', 'REEMA', 'MANOJ'],
                    'EMP_SALARY': [67000,34000,68000,90000,43000]
                    }
              df=pandas.DataFrame(EMP, index=['001','002', '003','004','005])
              print(df.loc[0:3,:])
               and she wants to get the following output:
              EMPID           EMPNAME         EMP_SALARY
              001  E01        KISHORI         67000
              002  E02        PRIYA           34000
              003  E03        DAMODAR         68000
               Help her to correct the code.
              a.  print(df.iloc['001':'003',:])               b. print(df.loc['001': '003',:])
              c.  print(EMP[loc[0:3,:]])                      d. print(df.loc['001' :'004',:])
         Ans.  b. print(df.loc['001': '003',:])


                                        NCERT Exercise Solutions


           1.  What is a DataFrame and how is it different from a 2-D array?
         Ans.   A Pandas DataFrame is a two-dimensional tabular structure that can accommodate objects of various types. while a NumPy
              2D array is a homogeneous, fixed-size array with numerical elements, lacking labeled axes and advanced data manipulation
              functionalities.
           2.  How are DataFrames related to Series?
         Ans.   A DataFrame in Pandas is a two-dimensional, tabular data structure made up of multiple Series. A Series, on the other
              hand, is a one-dimensional labelled array that can hold any type of data. Each column in a DataFrame is essentially a Series,
              with a common index. As a result, a DataFrame can be thought of as a collection of Series with the same index.
           3.  What do you understand by the size of a DataFrame?
         Ans.   The size of a DataFrame in Pandas refers to the total number of elements it contains, which is calculated as the product of
              the number of rows and columns.
           4.  Create the following DataFrame Sales containing year wise sales figures for five sales persons in INR. Use the years as
              column labels, and sales person names as row labels.
                                          2014           2015              2016        2017
                                 Madhu    100.5          12000            20000       50000
                                 Kusum    150.8          18000            50000       60000
                                Kinshuk   200.9          22000            70000       70000
                                  Ankit   30000          30000            100000      80000
                                 Shruti   40000          45000            125000      90000

         Ans.  import pandas as pd
              dict1 = {2014:[100.5,150.8,200.9,30000,4000],
                  2015:[12000,18000,22000,30000,45000],
                  2016:[20000,50000,70000,10000,125000],
                  2017:[50000,60000,70000,80000,90000]}
              Sales=pd.DataFrame(dict1,
              index=['Madhu',"Kusum","Kinshuk","Ankit","Shruti"])
              print(Sales)

          82   Touchpad Informatics Practices-XII
   91   92   93   94   95   96   97   98   99   100   101