Page 212 - Ai_V3.0_c11_flipbook
P. 212

Program 43: To create a DataFrame using list of dictionaries

                   import pandas as pd

                   # Using a List of Dictionaries
                   data = [{'ID': 1, 'Name': 'Arti', 'Age': 25},

                           {'ID': 2, 'Name': 'Trinabh', 'Age': 30},

                           {'ID': 3, 'Name': 'Surbhi', 'Age': 28}]
                   df = pd.DataFrame(data)
                   print(df)
                  Output:

                       ID        Name       Age
                  0     1        Arti        25
                  1     2    Trinabh         30
                  2     3      Surbhi        28

              Rows and Columns in DataFrame
              A DataFrame is a core data structure in Pandas, providing a tabular representation of data with rows and columns.
              This two-dimensional format mirrors the structure of a typical spreadsheet or a database table. Each column in a
              DataFrame represents a different variable or feature, while each row corresponds to a single observation or data point
              or record.
              You can perform some fundamental operations on rows and columns, which are as follows:
               • •    Selection: DataFrames allow us to select specific rows or columns based on various criteria. This selection can be
                  done using labels (column names) or indices (row numbers) or through boolean indexing based on conditions.
               • •    Addition: New columns or rows can be added to a DataFrame to incorporate additional information or derived
                  features. These additions can be based on existing data within the DataFrame or computed using external sources.
               • •    Deletion: DataFrames enable the removal of rows or columns that are not needed for analysis. This process helps
                  in cleaning and preparing the data for further processing.

              To perform these operation on DataFrame, we need to first create a DataFrame.

                Program 44: To create a sample DataFrame

                   # Import pandas library
                   import pandas as pd
                   # Define a dictionary containing employee data
                   data = {'Name':['Adit', 'Ekam', 'Sakshi', 'Anu'],

                           'Age':[27, 24, 25, 30],
                           'Address':['Delhi', 'Kanpur', 'Meerut', 'Indore'],
                           'Qualification':['M.Sc.', 'MA', 'MCA', 'Ph.D.']}
                   # Convert the dictionary into DataFrame
                   df = pd.DataFrame(data)

                   # Displaying DataFrame
                   print(df)


                    210     Touchpad Artificial Intelligence (Ver. 3.0)-XI
   207   208   209   210   211   212   213   214   215   216   217