How to create Pandas DataFrame | Create DataFrame from List, Array, Dict

How to create Pandas DataFrame

How to create Pandas DataFrame

 

Python Pandas DataFrame

A DataFrame is a two-dimensional array. In Data Frame data is stored in table form in rows and columns. Data Frame is size mutable means we can change the size of a DataFrame at any time. Indexes such as rows and columns and potentially columns are of different types.

Example :-

How to Create pandas Data Frame

 

1. Create pandas DataFrame from a list

The DataFrame can be created using a single list or list of lists.

Example 1.

import pandas as pd
data= [1,2,3,4,5]
df=pd.DataFrame (data)
print (df)

its output will be

          0
一一一
0        1
1        2
2        3
3        4
4        5
 
Note:- You can clearly see the result shows only one column whose name is not given by us it is by default 0, and indexes are also provided by default.

Example 2.

import pandas as pd
data= [ [‘Rahul’,15], [‘Amit’, ’13’], [‘Raj’, 16], [‘Keshu’, 14] ]
df=pd.DataFrame (data,columns=[‘Name’, ‘Age’])
print (df)

its output will be

          Name        Age
      一一一一一一一一
0        Rahul        15
1        Amit          13
2        Raj            16
3        Keshu        14
 
Note:- In this output, you can see there are 4 lists and column names are provided “Name” and “Age”. But indexes are provided by default.
 

2. Creating a DataFrame from a Dictionary of array

All the array must be of the same length. If the index is passed, then the length of the index must be equal to the length of the arrays. If no index is passed, then by default, the index will be range(n), where n is the length of the array.
 
Example 1.

import pandas as pd
data= {‘Name’ : [‘Rahul’, ‘Keshu’, ‘Amit’, ‘Yogesh’, ‘Raj’], ‘Age’ : [15,14,19,16,15] }
df=pd.DataFrame(data)
print (df)

Its output will be
          Name          Age
       一一一一一一一一
0        Rahul           15
1        Keshu          14
2        Amit            19
3        Yogesh        16
4        Raj              15
 
Note:- You can see there are two Dictionary “Name” and “Age”. but indexes are from values 0,1,2.3.4. They are provided by default using the range(n) function.
 
Example 2.
Create an Indexed DataFrame

import pandas as pd
data= {‘Name’ : [‘Rahul’, ‘Keshu’, ‘Amit’, ‘Yogesh’, ‘Raj’], ‘Age’ : [15,14,19,16,15] }
df=pd.DataFrame(data, index=[‘r1′,’r2′,’r3′,’r4′,’r5’])
print (df)

Its output will be
          Name          Age
       一一一一一一一一
r1       Rahul           15
r2       Keshu          14
r3       Amit            19
r4       Yogesh        16
r5       Raj              15
 
Note:- You can see there are two Dictionary “Name” and “Age”. and indexes are r1,r2,r3,r4 and r5
 

3. Creating a DataFrame from a List of Dictionary 

A list of Dictionaries can be passed as input data to create a DataFrame. The dictionary keys are by default taken as column names.
 
Example 1. The following example shows how to create a DataFrame by passing a list of dictionaries.

import pandas as pd
data = [ { ‘a’ : 10, ‘b’ : 5 },  { ‘c’ : 15, ‘d’ : 20,},
{ ‘a’ : 30, ‘b’ : 14,  ‘b’ : 25, ‘c’ : 50 } ]
df=pd.DataFrame(data)
print (df)

its output will be 
           a            b            c            d
      一一一一一一一一一一一一一一
0        10            5         NaN      NaN
1       NaN      NaN       15           20
2        30          25          50        NaN
 
Note:- You can see there are three Dictionaries. NaN values are filled in the missing areas. But indexes are provided by default using range(n) function.
 
Example 2. The following example shows how to create a Data Frame by passing a List of Dictionaries and the row indices.

import pandas as pd
data = [ { ‘a’ : 10, ‘c’ : 5 },  { ‘b’ : 15, ‘d’ : 20,},
{ ‘a’ : 30, ‘b’ : 14,  ‘b’ : 25, ‘c’ : 50 } ]
df=pd.DataFrame(data, index = [ ‘row1’, ‘row2’, ‘row3’ ] )
print (df)

 
its output will be 
                   a            c            b           d
              一一一一一一一一一一一一一
row1          10          5           NaN        NaN
row2         NaN       NaN       15            20
row3           30         50         25          NaN
 
Note:- You can see there are three Dictionaries. NaN values are filled in the missing areas. And indexes are row1, row2, and row3.
 
Example 3. The following example shows how to create a DataFrame with a list of dictionaries, row indices, and column indices.

import pandas as pd
data = [ { ‘a’ : 10, ‘b’ : 20 },  { ‘a’ : 15, ‘b’ : 30} ]
df1=pd.DataFrame(data, index = [ ‘row1’, ‘row2’ ], columns=[ ‘a’, ‘b’ ] )
df2=pd.DataFrame(data, index = [ ‘row1’, ‘row2’ ], columns=[ ‘a’, ‘z’ ] )
print (df1)
print (df2)

its output will be 
                   a           b
                一一一一一一
row1          10         20
row2          15         30
 
 
                   a           z
一一一一一一
row1          10         NaN
row2          15         NaN
 
Note:- You can see there are two Data Frame objects and two Dictionaries. NaN values are filled in the missing areas. And indexes are row1 and row2.
 

4. Creating a DataFrame from Dictionary of Series

Dictionary of Series can be passed to a DataFrame, The resultant index is the union of all the series indexes passed.
 
Example 

import pandas as pd
data = { ‘First’ : pd.Series ( [ 1,2,3 ], index= [‘a’, ‘b’, ‘c’ ] ),
‘Second’ : pd.Series ( [ 10,20,30 ], index=[‘a’, ‘b’, ‘c’ ] ) }
df=pd.DataFrame(data)
print (df)

its output will be 
 
        First        Second
      一一一一一一一一
a        1                10
b        2                20
c        3                30
 
Note:- You can see there are two Series “First” and “Second”. And indexes are labeled as a,b, and c.
 
 
 If you like my post please share it with your friends by simply clicking the below social media button, and don’t forget to comment so we will update ourselves.
Share in a Single Click

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *