Convert Nested Dictionary to CSV Table

后端 未结 3 1202
鱼传尺愫
鱼传尺愫 2020-12-19 23:30

I\'m going through a data mining tutorial and I\'m using the following dictionary.

users = {
    \"Angelica\": {
        \"Blues Traveler\": 3.5, 
        \"         


        
相关标签:
3条回答
  • 2020-12-20 00:01
    import pandas as pd
    data = pd.DataFrame(users)
    data = data.fillna("-")
    data.to_csv("./users.csv")
    
    0 讨论(0)
  • 2020-12-20 00:02

    You'll have to transpose from columns containing rows to rows containing columns. Using a collections.defaultdict() object would be easiest here:

    rows = defaultdict(dict)
    
    for user, artists in users.iteritems():
        for artist, count in artists.iteritems():
            rows[artist][user] = count
    

    Now you have dictionaries that can be written directly to a csv.DictWriter():

    with open(csvfilename, 'wb') as outf:
        writer = csv.DictWriter(outf, [''] + users.keys())
        writer.writeheader()
        writer.writerows(dict(row, **{'': key}) for key, row in rows.iteritems()) 
    

    The generator expression is needed to give each value in the rows dictionary the added first column key-value pair.

    Demo:

    >>> from collections import defaultdict
    >>> import csv
    >>> users = { ... }  # elided for brevity
    >>> rows = defaultdict(dict)
    >>> for user, artists in users.iteritems():
    ...     for artist, count in artists.iteritems():
    ...         rows[artist][user] = count
    ... 
    >>> import sys
    >>> writer = csv.DictWriter(sys.stdout, [''] + users.keys())
    >>> writer.writeheader()
    ,Angelica,Veronica,Sam,Jordyn,Dan,Bill,Chan,Hailey
    >>> writer.writerows(dict(row, **{'': key}) for key, row in rows.iteritems()) 
    The Strokes,2.5,3.0,5.0,4.0,4.0,,,4.0
    Blues Traveler,3.5,3.0,5.0,,3.0,2.0,5.0,
    Phoenix,5.0,4.0,5.0,5.0,3.0,2.0,5,
    Broken Bells,2.0,,2.0,4.5,4.0,3.5,1.0,4.0
    Deadmau5,,,,4.0,4.5,4.0,1.0,1.0
    Norah Jones,4.5,5.0,3.0,5.0,,,3.0,4.0
    Slightly Stoopid,1.5,2.5,4.0,4.5,4.5,3.5,1.0,
    Vampire Weekend,2.0,,,4.0,2.0,3.0,,1.0
    
    0 讨论(0)
  • 2020-12-20 00:19

    Try this

    import csv
    # Create header line
    a = ["Album/Track"] + users.keys()
    
    # Create unique keys.
    x = list(set([y for z in users.values() for y in z.keys()]))
    
    # Create rows
    rows = [a]+[[q]+[users[p].get(q, '-') for p in a[1:]] for q in x]
    
    with open('my.csv', 'wb') as csvfile:
        writer = csv.writer(csvfile)
        for row in rows:
            writer.write(row)
    
    0 讨论(0)
提交回复
热议问题