. Advertisement .
..3..
. Advertisement .
..4..
Converting a list of dictionaries to DataFame in Pandas is quite a challenge, but it is not as difficult as you might think. These guidelines will give you some helpful tips on this task. Keep scrolling for more.
Tips to Convert A List Of Dictionaries to DataFrame
Method 1. Use DataFrame ()
DataFrame ()
is among the quickest and most traditional methods. Each of your dictionaries represents a DataFrame record, as the keys turn into columns. In the example below, each of the dictionaries encompasses every key:
# Converting a List of Dictionaries to a DataFrame
import pandas as pd
list_of_dicts = [
{'Name': 'Nick', 'Age': 31, 'City': 'Toranto'},
{'Name': 'Katy', 'Age': 31, 'City': 'Landan'},
{'Name': 'Elvi', 'Age': 35, 'City': 'Landan'}]
df = pd.DataFrame(list_of_dicts)
print(df)
Our output will be like this:
# Returns:
# Name Age City
# 0 Nick 31 Toranto
# 1 Katy 31 Landan
# 2 Elvi 35 Landan
Aside from “df = pd.DataFrame(list_of_dicts)
“, you can try these out, too, which also work well:
df = pd.DataFrame.from_dict(list_of_dicts)
df = pd.DataFrame(list_of_dicts)
df = pd.DataFrame.from_records(list_of_dicts)
Method 2. Use From_Dict()
The method above is sufficient if no key is missing. But suppose there is a dictionary with one missing key; the problem might become a bit trickier. In that case, from_dict()
will be your best bet. By using them, you can read the dictionary list and translate them properly into a DataFrame with no trouble.
# Reading Dictionaries with Missing Keys
import pandas as pd
list_of_dicts = [{'Name': 'Nick', 'Age': 31, 'City': 'Toranto'},
{'Name': 'Katy', 'Age': 31, 'City': 'Landan'},
{'Name': 'Elvi', 'Age': 35}]
df = pd.DataFrame.from_dict(list_of_dicts)
print(df)
The output will be more or less the same except for the last row:
# Returns:
# Name Age City
# 0 Nick 31 Toranto
# 1 Katy 31 Landan
# 2 Elvi 35 NaN
As you can see, the output is not different from that of Method 1, regardless of whether you use from_records()
, from_dict()
, or pd.DataFrame()
. Dictionaries with missing keys will send back those missing values, NaN.
Method 3. Use Columns=
Sometimes, you only want to convert a column subset instead of the whole dictionary. The parameter “Columns=
” is the most suitable choice here, though you should remember that it’s only accessible in from_records()
and DataFrame()
constructors. If you try to apply this parameter in from_dict()
, a ValueError message will appear.
This example might help clear some clouds. We will use the same dictionary list, but this time, we only read two columns:
# Reading only a subset of columns
import pandas as pd
list_of_dicts = [{'Name': 'Nick', 'Age': 31, 'City': 'Toranto'},
{'Name': 'Katy', 'Age': 31, 'City': 'Landan'},
{'Name': 'Elvi', 'Age': 35}]
df = pd.DataFrame.from_records(list_of_dicts, columns=['Name', 'Age'])
# Same as: df = pd.DataFrame(list_of_dicts, columns=['Name', 'Age'])
print(df)
You will receive these outputs:
# Returns:
# Name Age
# 0 Nick 31
# 1 Katy 31
# 2 Elvi 35
Conclusion
Our guide has introduced three methods to convert a list of dictionaries to DataFrame. Our team believes you will have no trouble with these tasks from now on. Now you know how to read dictionaries into DataFrames. But is it possible to convert DataFrames into dictionaries? Of course, the answer is a Yes! Turn to this article for more guidance. You can learn about other similar issues on our website, too.
Leave a comment