Page 224 - Ai Book - 10
P. 224

Here, you can see that two columns are displayed among which the first column denotes the indexes for the
            elements and the second denotes the elements of the series.
            As you  have  read earlier,  Pandas  DataFrame  can be seen  as a table because it is a two-dimensional  data
            structure. It organizes data into rows and columns. In Python,two different methods are available to create
            Pandas DataFrame:

             u   By typing the values in Python itself to create the DataFrame: You can easily create a dataframe in Python
                by typing the values with the help of the following syntax:

                 import pandas as pd

                 data = {‘First Column Name’:  [‘First value’, ‘Second value’,...],
                         ‘Second Column Name’: [‘First value’, ‘Second value’,...],
                          ....
                         }
                 df = pd.DataFrame (data, columns = [‘First Column Name’,’Second Column Name’,...])
                 print (df)
            Let us understand the following syntax with the help of a simple example given below:

                 import pandas as pd

                 jeans = {‘Brand_Name’: [‘Killer’,’Allen Solly’,’Denim’],
                         ‘Price’: [20000,30000,80000],
                          ‘Discounted_Price’: [10000,25000,27000]
                         }
                 data_frame = pd.DataFrame(jeans, columns = [‘Brand_Name’, ‘Price’, ‘Discounted_
                 Price’])
                 print (data_frame)
            The output of the following lines of code is shown in the snapshot given below:














             u   By importing the values from a file (such as an Excel file), and then creating the DataFrame in Python
                based on the values imported: In Jupyter Notebook, you can easily access the values from a file that is
                stored on your computer. These values are used to create a dataframe. Now, let us learn how to create a
                dataframe using imported values from a file.
            The basic syntax is used to import an Excel file into Python in order to create a DataFrame is as follows:
                 import pandas as pd
                 data = pd.read_excel(r’File Location\File name.xlsx’)

                98
                98
   219   220   221   222   223   224   225   226   227   228   229