'convert list of dictionaries with values in list to a dataframe
How do i convert the below to a dataframe as like the expected output below? Please help. I tried other answers of SO but they were in different format of input.
ab = [{'q1':[7,2,6]},{'q2':[1,2,3]}]
import pandas as pd
pd.DataFrame(ab)
Current output:
q1 q2
0 [7, 2, 6] NaN
1 NaN [1, 2, 3]
Expected Output
q1 q2
0 7 1
1 2 2
2 6 3
Solution 1:[1]
Other options.
Using pandas.concat:
df = pd.concat(map(pd.DataFrame, ab), axis=1)
or using collections.ChainMap:
from collections import ChainMap
df = pd.DataFrame(dict(ChainMap(*ab)))
output:
q1 q2
0 7 1
1 2 2
2 6 3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
