Javatpoint Logo
Javatpoint Logo

How to create a DataFrames in Python

A Data Frame is a two-dimension collection of data. It is a data structure where data is stored in tabular form. Datasets are arranged in rows and columns; we can store multiple datasets in the data frame. We can perform various arithmetic operations, such as adding column/row selection and columns/rows in the data frame.

We can import the DataFrames from the external storage; these storages can be referred to as the SQL Database, CSV file, and an Excel file. We can also use the lists, dictionary, and from a list of dictionary, etc.

In this tutorial, we will learn to create the data frame in multiple ways. Let's understand these different ways.

First, we need to install the pandas library into the Python environment.

An empty dataframe

We can create a basic empty Dataframe. The dataframe constructor needs to be called to create the DataFrame. Let's understand the following example.

Example -

Output:

Empty DataFrame
Columns: []
Index: []

Method - 2: Create a dataframe using List

We can create dataframe using a single list or list of lists. Let's understand the following example.

Example -

Output:

0        Java
1      Python
2           C
3         C++
4   JavaScript
5       Swift
6          Go

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

The dict of ndarray/lists can be used to create a dataframe, all the ndarray must be of the same length. The index will be a range(n) by default; where n denotes the array length. Let's understand the following example.

Example -

Output:

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

Method - 4: Create a indexes Dataframe using arrays

Let's understand the following example to create the indexes dataframe using arrays.

Example -

Output:

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

Explanation -

In the above code, we have defined the column name with the various car names and their ratings. We used the array to create indexes.

Method - 5: Create Dataframe from list of dicts

We can pass the lists of dictionaries as input data to create the Pandas dataframe. The column names are taken as keys by default. Let's understand the following example.

Example -

Output:

    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 understand another example to create the pandas dataframe from list of dictionaries with both row index as well as column index.

Example - 2:

Output:

             x    y
first   1.0   2.0
second  NaN NaN 

             x    y1
first   1.0 NaN
second NaN NaN

Let's understand another example to create dataframe by passing lists of dictionary and rows.

Example - 3

Output:

         x     y   z
first    2   NaN   3
second  10  20.0  30

We have discussed the three ways to create the dataframe using the lists of dictionary.

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

The zip() function is used to merge the two lists. Let's understand the following example.

Example -

Output:

[('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

The dictionary can be passed to create a dataframe. We can use the Dicts of series where the subsequent index is the union of all the series of passed index value. Let's understand the following example.

Example -

Output:

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

In this tutorial, we have discussed the different ways to create the DataFrames.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA