Convert list of dictionaries to a pandas DataFrame
Here is an example of how to convert a list of dictionaries to a pandas DataFrame:
import pandas as pd
list_of_dicts = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4, 'c': 5}]
df = pd.DataFrame(list_of_dicts)
print(df)
This will output:
a b c 0 1 2 NaN 1 3 4 5.0
Watch a video course
Python - The Practical Guide
In this example, the list of dictionaries list_of_dicts
is passed as the argument to the pd.DataFrame()
function, which converts the list of dictionaries to a pandas DataFrame.
If you want to specify columns name, you can use pd.DataFrame.from_records(list_of_dicts, columns=['col1', 'col2', 'col3'])