Create a DataFrame from List of Dicts

Create a DataFrame from List of Dicts

List of Dictionaries can be passed as input data to create a DataFrame. The dictionary keys are by default taken as column names.

import pandas as pd
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
df = pd.DataFrame(data)
df
a b c
0 1 2 NaN
1 5 10 20.0

Observe, NaN (Not a Number) is appended in missing areas.

df.dropna()
a b c
1 5 10 20.0

to drop or delete the row temporarily. dropna() - drops rows having NaN val

Create a DataFrame by passing a list of dictionaries and the row indices

data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
df = pd.DataFrame(data, index=['first', 'second'])
df
a b c
first 1 2 NaN
second 5 10 20.0

null values filles with mean values of the column

df.c.fillna(df.c.mean(), inplace=True)
df
a b c
first 1 2 20.0
second 5 10 20.0

Create a DataFrame with a list of dictionaries, row indices, and column indices

data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
#With two column indices, values same as dictionary keys
df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b'])
#With two column indices with one index with other name
df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1'])
df1
a b
first 1 2
second 5 10
df2
a b1
first 1 NaN
second 5 NaN

df2 DataFrame is created with a column index other than the dictionary key; thus, appended the NaN’s in place. Whereas, df1 is created with column indices same as dictionary keys, so NaN’s appended.