Create a Pandas DataFrame from List of Dicts

Create a Pandas DataFrame from List of Dicts

Creating a Pandas DataFrame from a list of dictionaries is straightforward. Each dictionary in the list corresponds to a row in the DataFrame. The keys in the dictionary become the column names, and the associated values become the data for those columns.

Here's a step-by-step guide:

1. Install and Import Pandas:

If you haven't already, you'll need to install Pandas:

pip install pandas 

Then, import the necessary library:

import pandas as pd 

2. Create the List of Dictionaries:

Let's assume you have the following list of dictionaries:

data = [ {'Name': 'John', 'Age': 28, 'City': 'New York'}, {'Name': 'Anna', 'Age': 22, 'City': 'London'}, {'Name': 'Mike', 'Age': 32, 'City': 'San Francisco'} ] 

3. Convert the List of Dictionaries to a DataFrame:

Use the pd.DataFrame() constructor:

df = pd.DataFrame(data) 

4. Display the DataFrame:

You can print the DataFrame to view its contents:

print(df) 

This will produce:

 Name Age City 0 John 28 New York 1 Anna 22 London 2 Mike 32 San Francisco 

And that's it! You've successfully created a Pandas DataFrame from a list of dictionaries.


More Tags

automatic-ref-counting bag uitabbaritem vue-component google-cloud-functions str-replace cygwin transform transactionmanager gdal

More Programming Guides

Other Guides

More Programming Examples