Skip to content

Instantly share code, notes, and snippets.

@adamgreenhall
Forked from ameliagreenhall/df2json.py
Last active October 12, 2019 21:13
Show Gist options
  • Save adamgreenhall/4755513 to your computer and use it in GitHub Desktop.
Save adamgreenhall/4755513 to your computer and use it in GitHub Desktop.

Revisions

  1. adamgreenhall revised this gist Feb 11, 2013. 1 changed file with 5 additions and 23 deletions.
    28 changes: 5 additions & 23 deletions df2json.py
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,11 @@
    """
    tiny script to convert a pandas data frame into a JSON object
    orig: https://gist.github.com/1486027 via Mike Dewar
    Test data:
    df = pandas.DataFrame({
    "time" : [1,2,3,4,5],
    "temp" : np.random.rand(5)
    })
    """

    import json as json

    def to_json(df,filename):
    d = [
    dict([
    (colname, row[i])
    for i,colname in enumerate(df.columns)
    ])
    for row in df.values
    ]
    return json.dump(d, open(filename + '.json', 'w'))

    to_json(df, 'my_filename')

    # Preview file:
    !head my_filename.json
    def df_to_json(df, filename=''):
    x = df.reset_index().T.to_dict().values()
    if filename:
    with open(filename, 'w+') as f: f.write(json.dumps(x))
    return x
  2. @ameliagreenhall ameliagreenhall revised this gist Feb 4, 2013. 1 changed file with 20 additions and 12 deletions.
    32 changes: 20 additions & 12 deletions df2json.py
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,29 @@
    """
    tiny script to convert a pandas data frame into a JSON object
    """
    orig: https://gist.github.com/1486027 via Mike Dewar
    import ujson as json
    import pandas
    import numpy as np
    Test data:
    df = pandas.DataFrame({
    "time" : [1,2,3,4,5],
    "temp" : np.random.rand(5)
    })
    d = [
    dict([
    (colname, row[i])
    for i,colname in enumerate(df.columns)
    ])
    for row in df.values
    ]
    return json.dumps(d)
    """

    import json as json

    def to_json(df,filename):
    d = [
    dict([
    (colname, row[i])
    for i,colname in enumerate(df.columns)
    ])
    for row in df.values
    ]
    return json.dump(d, open(filename + '.json', 'w'))

    to_json(df, 'my_filename')

    # Preview file:
    !head my_filename.json
  3. @mikedewar mikedewar revised this gist Jan 18, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion df2json.py
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    tiny script to convert a pandas data frame into a JSON object
    """

    import json
    import ujson as json
    import pandas
    import numpy as np

  4. @mikedewar mikedewar created this gist Dec 16, 2011.
    21 changes: 21 additions & 0 deletions df2json.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    """
    tiny script to convert a pandas data frame into a JSON object
    """

    import json
    import pandas
    import numpy as np

    df = pandas.DataFrame({
    "time" : [1,2,3,4,5],
    "temp" : np.random.rand(5)
    })

    d = [
    dict([
    (colname, row[i])
    for i,colname in enumerate(df.columns)
    ])
    for row in df.values
    ]
    return json.dumps(d)