Warning: session_start(): open(/tmp/sess_b4ceec238aceb7cc031e195042843d90, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Python list of dictionaries to dataframe - LearnShareIT

Python list of dictionaries to dataframe

Python list of dictionaries to dataframe

In the Python programming language, the dataframe is a popular data type to work with data tables in the data science field. In many cases, creating a dataframe from a list of dictionaries is necessary. Today, our article will give you some helpful knowledge about Python list of dictionaries to dataframe.

What is the list of dictionaries?

A dictionary is an object that has one or many pairs of keys and values. Visit our previous article to understand more about the list of dictionaries. 

Python list of dictionaries to dataframe

The pandas is the most helpful library when working with data tables. It is easy to create a dataframe by calling pandas’ APIs. To create a dataframe from a list of dictionaries, we can use one of three following methods: DataFrame(), DataFrame.from_dict(), and DataFrame.from_records().

Below are detailed implementations for the methods.

Using pd.DataFrame()

The function creates a dataframe from an n-dimension array, dataframe, dictionary, or iterable object.

Code:

import pandas as pd
 
data = [{"Name": "John", "Age": 7, "Hair_color": "Black", "State": "Florida"},
        {"Name": "Alex", "Age": 8, "Hair_color": "Yellow", "State": "Ohio"},
        {"Name": "Brian", "Age": 16, "Hair_color": "Black", "State": "Texas"},
        {"Name": "Peter", "Age": 16, "Hair_color": "Yellow", "State": "New York"},
        {"Name": "Adam", "Age": 13, "Hair_color": "Brown", "State": "North Carolina"}]
 
# Create a dataframe from the list of dictionaries
df = pd.DataFrame(data)
 
print(df)

Result:

    Name  Age  Hair_color           State
0   John    7       Black         Florida
1   Alex    8      Yellow            Ohio
2  Brian   16       Black           Texas
3  Peter   16      Yellow        New York
4   Adam   13       Brown  North Carolina

Using pd.DataFrame.from_dict()

The function creates a dataframe from a dictionary or sequence of dictionaries.

Code:

import pandas as pd
 
data = [{"Name": "John", "Age": 7, "Hair_color": "Black", "State": "Florida"},
        {"Name": "Alex", "Age": 8, "Hair_color": "Yellow", "State": "Ohio"},
        {"Name": "Brian", "Age": 16, "Hair_color": "Black", "State": "Texas"},
        {"Name": "Peter", "Age": 16, "Hair_color": "Yellow", "State": "New York"},
        {"Name": "Adam", "Age": 13, "Hair_color": "Brown", "State": "North Carolina"}]
 
# Create a dataframe from the list of dictionaries
df = pd.DataFrame.from_dict(data)
 
print(df)

Result:

    Name  Age  Hair_color           State
0   John    7       Black         Florida
1   Alex    8      Yellow            Ohio
2  Brian   16       Black           Texas
3  Peter   16      Yellow        New York
4   Adam   13       Brown  North Carolina

Using pd.DataFrame.from_records()

The method creates a dataframe from a structured n – dimension array, dataframe, or sequence of dictionaries or tuples.

Code:

import pandas as pd

data = [{"Name": "John", "Age": 7, "Hair_color": "Black", "State": "Florida"},
        {"Name": "Alex", "Age": 8, "Hair_color": "Yellow", "State": "Ohio"},
        {"Name": "Brian", "Age": 16, "Hair_color": "Black", "State": "Texas"},
        {"Name": "Peter", "Age": 16, "Hair_color": "Yellow", "State": "New York"},
        {"Name": "Adam", "Age": 13, "Hair_color": "Brown", "State": "North Carolina"}]
 
# Create a dataframe from the list of dictionaries
df = pd.DataFrame.from_records(data)
 
print(df)

Result:

    Name  Age  Hair_color           State
0   John    7       Black         Florida
1   Alex    8      Yellow            Ohio
2  Brian   16       Black           Texas
3  Peter   16      Yellow        New York
4   Adam   13       Brown  North Carolina

Summary

In summary, you can easily filter a list of dictionaries by flexibly accessing their keys and values. If the dictionaries have the same format of keys, access them with the keys. Else, access them by the items attribute.

Maybe you are interested:

Leave a Reply

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