Page 202 - Touhpad Ai
P. 202

df = pd.DataFrame(data, columns=['Name', 'Age', 'City'])
                 print(df)
                 Output:
                        Name         Age     City
                0           1     Mayur        25
                1           2      Esha      30
                2           3    Vedant      28
              u  From a dictionary of lists or arrays: You can create a DataFrame from a dictionary where keys are column names
                  and values are lists or arrays containing the data for each column.

                  Program 6: To create a DataFrame using dictionary of lists or arrays

                 import pandas as pd
                 data = {'Name': ['Yash', 'Rehan', 'Neha'],
                         'Age': [25, 30, 28],
                         'City': ['Delhi', 'Mumbai', 'Bengaluru']}
                 df = pd.DataFrame(data)
                 print(df)
                 Output:
                    Name  Age       City
                0   Yash   25      Delhi
                1  Rehan   30     Mumbai
                2   Neha   28  Bengaluru
              u  Using NumPy ndarrays: Creating DataFrames from NumPy ndarrays allows you to leverage the power of both
                  libraries for efficient data processing and analysis. NumPy provides efficient array computations, while Pandas offers
                  powerful data manipulation and analysis tools.

                  Program 7: To create a DataFrame using ndarray

                 import pandas as pd
                 import numpy as np
                 # Using NumPy ndarray
                 data = np.array([[1, 'Mayur', 25],
                                  [2, 'Esha', 30],
                                  [3, 'Vedant', 28]])
                 df = pd.DataFrame(data, columns=['ID', 'Name', 'Age'])
                 print(df)

              Output:
                       ID        Name       Age
                0       1       Mayur        25
                1       2        Esha        30

                2       3     Vedant         28
              u  Using a List of Dictionaries: You can create a DataFrame from a list of dictionaries where each dictionary represents
                  a row and keys represent column names.







                200     Touchpad Artificial Intelligence - XI
   197   198   199   200   201   202   203   204   205   206   207