Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»How to create a DataFrames in Python
    python

    How to create a DataFrames in Python

    March 24, 2023No Comments5 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Instructions for creating DataFrames in Python

    A Data Frame is a table of information with just two columns and rows. It’s a method of organising information wherein tabular records are used to store information. Several datasets may be stored in the same data frame, which is organised like a table with rows and columns. Many arithmetic operations are available, including the addition of a selected column or row to the corresponding column or row in the data frame.

    DataFrames may be imported from a variety of external sources, including SQL databases, comma-separated value (CSV) files, and Microsoft Excel spreadsheets. Another option is to utilise dictionaries, lists, etc.

    Many methods for constructing the data frame will be covered in this guide. Let’s go into these various approaches.

    It is necessary to add the pandas library to Python first.

    environment.

    An empty dataframe

    A simple, empty Dataframe may be made. In order to create a DataFrame, it is necessary to use the dataframe function Object() { [native code] }. Now, let’s analyse the following scenario. Namely –

    Resulting



    1. #


      import


      pandas as pd



    2. import


      pandas as pd



    3. # Calling DataFrame constructor

    4. df = pd.DataFrame()


    5. print(df)

    :

    Empty DataFrame
    Columns: []
    Index: []
    

    Method – 2: Create a dataframe using List

    A single list or a collection of lists may be used to construct a dataframe. Now, let’s analyse the following scenario. Namely –

    Its Output is



    1. # importing pandas library



    2. import


      pandas as pd



    3. # string values in the list

    4. lst = [

      ‘Java’


      ,


      ‘Python’


      ,


      ‘C’


      ,


      ‘C++’


      ,



    5. ‘JavaScript’


      ,


      ‘Swift’


      ,


      ‘Go’


      ]



    6. # Calling DataFrame constructor on list

    7. dframe = pd.DataFrame(lst)

    8. print(dframe)
    0        Java
    1      Python
    2           C
    3         C++
    4   JavaScript
    5       Swift
    6          Go
    

    Method – 3: Create Dataframe from dict of ndarray/lists

    In order to make a dataframe from a dict of ndarray/lists, all the ndarray must have the same length. By default, the index will be of type range(n), where n is the array’s size. Now, let’s analyse the following scenario. Namely –

    Resulting



    1. import


      pandas as pd



    2. # assign data of lists.

    3. data = {

      ‘Name’


      : [


      ‘Tom’


      ,


      ‘Joseph’


      ,


      ‘Krish’


      ,


      ‘John’


      ],


      ‘Age’


      : [


      20


      ,


      21


      ,


      19


      ,


      18


      ]}



    4. # Create DataFrame

    5. df = pd.DataFrame(data)


    6. # Print the output.

    7. print(df)

    :

         Name  Age
    0     Tom   20
    1  Joseph   21
    2   Krish   19
    3    John   18
    

    Method – 4: Create a indexes Dataframe using arrays

    Now, let’s figure out how to build an array-based index dataframe by analysing the following code snippet. Namely –

    Resulting



    1. # DataFrame using arrays.



    2. import


      pandas as pd



    3. # assign data of lists.

    4. data = {

      ‘Name’


      :[


      ‘Renault’


      ,


      ‘Duster’


      ,


      ‘Maruti’


      ,


      ‘Honda City’


      ],


      ‘Ratings’


      :[


      9.0


      ,


      8.0


      ,


      5.0


      ,


      3.0


      ]}



    5. # Creates pandas DataFrame.

    6. df = pd.DataFrame(data, index =[

      ‘position1’


      ,


      ‘position2’


      ,


      ‘position3’


      ,


      ‘position4’


      ])



    7. # print the data

    8. print(df)

    Justification:

                   Name      Ratings
    position1     Renault      9.0
    position2      Duster      8.0
    position3      Maruti      5.0
    position4    Honda City      3.0
    

    Column names including automobile names and star ratings have been defined in the preceding code. We built on the array’s foundation to

    indexes.

    Method – 5: Create Dataframe from list of dicts

    The dictionaries’ lists may be used as input for developing a Pandas dataframe. By default, the names of the columns themselves are used as the keys. Now, let’s analyse the following scenario. Namely –

    Resulting



    1. # the example is to create


    2. # Pandas DataFrame by lists of dicts.


    3. import


      pandas as pd



    4. # assign values to lists.

    5. data = [{

      ‘A’


      :


      10


      ,


      ‘B’


      :


      20


      ,


      ‘C’


      :


      30


      }, {


      ‘x’


      :


      100


      ,


      ‘y’


      :


      200


      ,


      ‘z’


      :


      300


      }]



    6. # Creates DataFrame.

    7. df = pd.DataFrame(data)


    8. # Print the data

    9. print(df)

    :

        A      B      C      x      y      z
    0  10.0  20.0  30.0    NaN    NaN    NaN
    1   NaN   NaN   NaN  100.0  200.0  300.0
    

    Let’s have a look at another another example of how to build a pandas dataframe from a list of dictionaries containing both row and column indices. As a second illustration:

    Resulting



    1. import


      pandas as pd



    2. # assigns values to lists.

    3. data = [{

      ‘x’


      :


      1


      ,


      ‘y’


      :


      2


      }, {


      ‘A’


      :


      15


      ,


      ‘B’


      :


      17


      ,


      ‘C’


      :


      19


      }]



    4. # With two column indices, values same

    5. # as dictionary keys

    6. dframe1 = pd.DataFrame(data, index =[

      ‘first’


      ,


      ‘second’


      ], columns =[


      ‘x’


      ,


      ‘y’


      ])



    7. # With two column indices with

    8. # one index with other name

    9. dframe2 = pd.DataFrame(data, index =[

      ‘first’


      ,


      ‘second’


      ], columns =[


      ‘x’


      ,


      ‘y1’


      ])



    10. # print the first data frame

    11. print (dframe1,

      “\n”


      )


    12. # Print the second DataFrame.

    13. print (dframe2)
                 x    y
    first   1.0   2.0
    second  NaN NaN 
                 x    y1
    first   1.0 NaN
    second NaN NaN
    

    Therefore, let’s learn how to construct a dataframe by giving in a dictionary and a list of rows. Instance 3

    As a result of



    1. # The example is to create


    2. # Pandas DataFrame by passing lists of

    3. # Dictionaries and row indices.


    4. import


      pandas as pd



    5. # assign values to lists

    6. data = [{

      ‘x’


      :


      2


      ,


      ‘z’


      :


      3


      }, {


      ‘x’


      :


      10


      ,


      ‘y’


      :


      20


      ,


      ‘z’


      :


      30


      }]



    7. # Creates padas DataFrame by passing

    8. # Lists of dictionaries and row index.

    9. dframe = pd.DataFrame(data, index =[

      ‘first’


      ,


      ‘second’


      ])



    10. # Print the dataframe

    11. print(dframe)

    Programming,

             x     y   z
    first    2   NaN   3
    second  10  20.0  30
    

    Three methods for constructing the dataframe from the aforementioned lists have been presented.

    dictionary.

    Method – 6: Create Dataframe using the zip() function

    To combine the two sets, we utilise the zip() method. Now, let’s analyse the following scenario. Namely –

    Resulting



    1. # The example is to create


    2. # pandas dataframe from lists using zip.



    3. import


      pandas as pd



    4. # List1

    5. Name = [

      ‘tom’


      ,


      ‘krish’


      ,


      ‘arun’


      ,


      ‘juli’


      ]



    6. # List2

    7. Marks = [

      95


      ,


      63


      ,


      54


      ,


      47


      ]



    8. #  two lists.

    9. # and merge them by using zip().

    10. list_tuples = list(zip(Name, Marks))


    11. # Assign data to tuples.

    12. print(list_tuples)


    13. # Converting lists of tuples into

    14. # pandas Dataframe.

    15. dframe = pd.DataFrame(list_tuples, columns=[

      ‘Name’


      ,


      ‘Marks’


      ])



    16. # Print data.

    17. print(dframe)

    :

    [('john', 95), ('krish', 63), ('arun', 54), ('juli', 47)]
        Name  Marks
    0   john     95
    1  krish     63
    2   arun     54
    3   juli     47
    

    Method – 7: Create Dataframe from Dicts of series

    A dataframe may be made with the help of the dictionary. Dicts of series may be used, with the next index being the concatenation of all the series indexed before. Now, let’s analyse the following scenario. Namely –

    The Results of the



    1. # Pandas Dataframe from Dicts of series.




    2. import


      pandas as pd



    3. # Initialize data to Dicts of series.

    4. d = {

      ‘Electronics’


      : pd.Series([


      97


      ,


      56


      ,


      87


      ,


      45


      ], index =[


      ‘John’


      ,


      ‘Abhinay’


      ,


      ‘Peter’


      ,


      ‘Andrew’


      ]),



    5. ‘Civil’


      : pd.Series([


      97


      ,


      88


      ,


      44


      ,


      96


      ], index =[


      ‘John’


      ,


      ‘Abhinay’


      ,


      ‘Peter’


      ,


      ‘Andrew’


      ])}



    6. # creates Dataframe.

    7. dframe = pd.DataFrame(d)


    8. # print the data.

    9. print(dframe)

    Computer:

            Electronics      Civil
    John             97        97
    Abhinay      56        88
    Peter           87        44
    Andrew      45        96
    

    Many methods for generating DataFrames have been covered in this lesson.

    How to create a DataFrames in Python Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleCreate and access package in python
    Next Article Python Practice Problems Get Ready for Your Next Interview

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.