Page 47 - Informatics_Practices_Fliipbook_Class12
P. 47

output:
                   CustomerName ItemPurchased  Cost
                 0       Ashish         Bread  22.5
                 1       Nikita    Vegetables  90.0
                 2        Vinod          Milk  75.0
            The attribute dtypes is used to retrieve the type of objects in various columns of a DataFrame, as shown below:

             >>> purchasesDF.dtypes
            output:
                 CustomerName      object
                 ItemPurchased     object
                 Cost             float64
                 dtype: object

             C T  01     1.  Consider the following nested list:
                               accounts = [[32345566, 'Savings', 5000], [43434567, 'Savings', 2000], [89897878, 'Current', -1000],
                             [67675454, 'Current', 10000]]
                               Write a statement that creates the following DataFrame with proper column labels:
                             Account Number      Account Type          Balance
                             0   32345566          Savings              5000
                             1   43434567          Savings              2000
                             2   89897878          Current              -1000
                             3   67675454          Current              10000
                         2.  For the DataFrame created in the previous question, display the type of values stored in each column.




                  Pandas DataFrame: Two-dimensional tabular structure that can accommodate objects of various types.
                  Dictionary, series or list of lists can be used to create Pandas DataFrame using pd.DataFrame() method.


            2.3 Reading from csv File

            In the above discussion, we have described how to create a DataFrame using a dictionary, list of lists, or a series.
            Although simple, these methods work only when we are dealing with a very small quantity of data. The data is often
            available in the form of spreadsheets (CSV files) that stores the data as comma-separated values. Many financial and
            accounting systems utilize CSV files for data exchange. Even several e-commerce platforms use CSV files to import and
            export product catalogues, update inventory, or export sales data.
            To  analyze  and  manipulate  the  data,  we  can  read  these  CSV  files  in  Pandas  DataFrame  using  the  method
            read_csv() of Pandas as shown below:

             >>> import pandas as pd
             >>> #Reading a CSV file into a DataFrame
             >>> groceryDF = pd.read_csv('Grocery.csv')
             >>> print(groceryDF)
            output:
                       Product   Category  Price  Quantity
                 0       Bread       Food     20         2
                 1        Milk       Food     60         5
                 2     Biscuit       Food     20         2
                 3  Bourn-Vita       Food     70         1
                 4        Soap    Hygiene     40         4
                 5       Brush    Hygiene     30         2
                 6   Detergent  Household     80         1
                 7     Tissues    Hygiene     30         5


                                                                             Data Handling using Pandas DataFrame  33
   42   43   44   45   46   47   48   49   50   51   52