How to convert multiple list of dictionaries plus a list into a dataframe
Question:
can anyone help with the following: I have a set of data output structured below
list = [0.2, 0.5]
dict1 = [{'a':13, 'b':16}, {'a':25, 'b':70}]
dict2 = [{'a':0.15, 'b':0.02}, {'a':0.30, 'b':0.24}]
The dictionary elements of the list of dictionary in dict1 and dict2 are the corresponding output of the values in the list. This means that 13 and 16 in dict 1 is as a result of the value 0.2 in the list variable.
I want to convert these values in a dataframe such that I would have something that looks like this:
index list dict1 dict2
a 0.2 13 0.15
b 0.2 16 0.02
a 0.5 25 0.30
b 0.5 70 0.24
Is there a way to go about this. The above is just an imaginative representation of how I think the dataframe could be. A better approach on how to present this in a dataframe is also appreciated
Answers:
Not the cleanest solution, but this would work:
import pandas as pd
lst = [0.2, 0.5]
dict1 = [{'a':13, 'b':16}, {'a':25, 'b':70}]
dict2 = [{'a':0.15, 'b':0.02}, {'a':0.30, 'b':0.24}]
df = pd.DataFrame()
for l, d1, d2 in zip(lst, dict1, dict2):
for d1i, d2i in zip(d1.items(), d2.items()):
df = df.append({'index': d1i[0], 'list': l, 'dict1': d1i[1], 'dict2': d2i[1]}, ignore_index=True)
df.set_index('index', inplace=True)
print(df.to_string())
Output:
list dict1 dict2
index
a 0.2 13 0.15
b 0.2 16 0.02
a 0.5 25 0.30
b 0.5 70 0.24
I’m positive this could be done as a oneliner, but I’m not too familiar with Pandas myself.
Alternative solution (list
renamed to lst
):
df = pd.concat(
pd.DataFrame({"list": v, "dict1": pd.Series(d1), "dict2": pd.Series(d2)})
for v, d1, d2 in zip(lst, dict1, dict2)
)
can anyone help with the following: I have a set of data output structured below
list = [0.2, 0.5]
dict1 = [{'a':13, 'b':16}, {'a':25, 'b':70}]
dict2 = [{'a':0.15, 'b':0.02}, {'a':0.30, 'b':0.24}]
The dictionary elements of the list of dictionary in dict1 and dict2 are the corresponding output of the values in the list. This means that 13 and 16 in dict 1 is as a result of the value 0.2 in the list variable.
I want to convert these values in a dataframe such that I would have something that looks like this:
index list dict1 dict2
a 0.2 13 0.15
b 0.2 16 0.02
a 0.5 25 0.30
b 0.5 70 0.24
Is there a way to go about this. The above is just an imaginative representation of how I think the dataframe could be. A better approach on how to present this in a dataframe is also appreciated
Not the cleanest solution, but this would work:
import pandas as pd
lst = [0.2, 0.5]
dict1 = [{'a':13, 'b':16}, {'a':25, 'b':70}]
dict2 = [{'a':0.15, 'b':0.02}, {'a':0.30, 'b':0.24}]
df = pd.DataFrame()
for l, d1, d2 in zip(lst, dict1, dict2):
for d1i, d2i in zip(d1.items(), d2.items()):
df = df.append({'index': d1i[0], 'list': l, 'dict1': d1i[1], 'dict2': d2i[1]}, ignore_index=True)
df.set_index('index', inplace=True)
print(df.to_string())
Output:
list dict1 dict2
index
a 0.2 13 0.15
b 0.2 16 0.02
a 0.5 25 0.30
b 0.5 70 0.24
I’m positive this could be done as a oneliner, but I’m not too familiar with Pandas myself.
Alternative solution (list
renamed to lst
):
df = pd.concat(
pd.DataFrame({"list": v, "dict1": pd.Series(d1), "dict2": pd.Series(d2)})
for v, d1, d2 in zip(lst, dict1, dict2)
)