Page 48 - Informatics_Practices_Fliipbook_Class12
P. 48

As shown above, the method read_csv() takes the path of the CSV file as input and returns a Pandas DataFrame
        object. For example, if we have a CSV file named Grocery.csv located in the same directory as our Python script, we
        can read it using the above mentioned code. By default, the read_csv() function assumes that the first row of the
        CSV file contains the column headers. If the CSV file does not have column headers, we can set the header parameter
        to None as follows:

         >>> import pandas as pd
         >>> groceryDF = pd.read_csv('Grocery.csv', header=None)
         >>> print(groceryDF)
        output:
                          0          1      2         3
              0     Product   Category  Price  Quantity
              1       Bread       Food     20         2
              2        Milk       Food     60         5
              3     Biscuit       Food     20         2
              4  Bourn-Vita       Food     70         1
              5        Soap    Hygiene     40         4
              6       Brush    Hygiene     30         2
              7   Detergent  Household     80         1
              8     Tissues    Hygiene     30         5
        2.3.1 Specifying the Delimiter

        Suppose, we now wish to read the content of the file grocery.txt as shown below:
                  Product;Category;Price;Quantity
                  Bread;Food;20;2
                  Milk;Food;60;5
                  Biscuit;Food;20;2
         >>> import pandas as pd
         >>> groceryDF = pd.read_csv('Grocery.txt')
         >>> print(groceryDF)
        output:
                Product;Category;Price;Quantity
              0                 Bread;Food;20;2
              1                  Milk;Food;60;5
              2               Biscuit;Food;20;2
        Note that when reading a text file (grocery.txt), each line of text is considered a single object i.e. a single string.
        The method pd.read_csv ignores the semicolons, because by default Pandas considers only a comma as a valid
        delimiter. However, we can explicitly specify the delimiter using either the delimiter or sep keyword argument
        while invoking the read_csv() function, as shown below:

         >>> import pandas as pd
         >>> groceryDF = pd.read_csv('Grocery.txt', delimiter=';')
         >>> print(groceryDF)
        output:
                 Product Category  Price  Quantity
              0    Bread     Food     20         2
              1     Milk     Food     60         5
              2  Biscuit     Food     20         2
         >>> import pandas as pd
         >>> groceryDF = pd.read_csv('Grocery.txt', sep=';')
         >>> print(groceryDF)
        output:
                 Product Category  Price  Quantity
              0    Bread     Food     20         2
              1     Milk     Food     60         5
              2  Biscuit     Food     20         2

          34   Touchpad Informatics Practices-XII
   43   44   45   46   47   48   49   50   51   52   53