Reputation: 5533
I have a list of dictionaries like this
[{"Key":[val1,val2,...]}, {"Key2":[val1,val2,...]}, ...]
I would like to convert this to a csv format where the keys are column headers, and he values form the column values
To do this I had intended to use a pandas
dataframe (which exports to csv readily), but I can't work out how to get my values into the data frame.
I've tried myPanda = pd.DataFrame.from_records(data)
but alas, to no avail.
Upvotes: 1
Views: 240
Reputation: 402423
I'm assuming your keys are unique, otherwise this wouldn't make much sense.
Option 1
pd.DataFrame
df.to_csv
dct = [{...}, {...}, ...]
r = {}
for d in dct:
r.update(d)
pd.DataFrame(r).to_csv('file.csv')
Option 2
Using cytoolz
, there's another possibility for dictionary merging -
from cytoolz.dicttoolz import merge
df = pd.DataFrame(merge(*dct))
Option 3
Create separate dataframes and concatenate them -
df = pd.concat(list(map(pd.DataFrame, dct)), axis=1)
This option works even if your lists are uneven.
Credit to piRSquared for the last two solutions!.
Upvotes: 4