Dinosaurius
Dinosaurius

Reputation: 8628

Convert each row of pandas DataFrame to a separate Json string

I use this code in order to convert each row of pandas DataFrame df into Json string. The problem is that it's printing None, however df.head() prints out the data.

import pandas as pd
import json

df = pd.read_csv('mydataset.csv')

for i in df.index:
    print df.loc[i].to_json("row{}.json".format(i))
    if i==10:
        break

How to get each row as a Json string variable and print it out? The Json string's structure is plain, no arrays, just string, integer and float fields.

Upvotes: 2

Views: 12770

Answers (5)

Chris
Chris

Reputation: 18876

to produce a json record for each dataframe:

for i in df.index:
    record = df.iloc[i].to_json()
    print(record)

Upvotes: 0

eXa
eXa

Reputation: 648

I wasn't happy with the solutions presented and ended up doing this:

for chunk in (pd.read_csv(file, sep = ",", header = False, index_col = 0, chunksize=chunk_size)):
    json_chunk = chunk.to_json(orient = "records", force_ascii = True, default_handler = None)

My solution does it with chunks as I'm reading millions of rows.

Upvotes: 1

htnawsaj
htnawsaj

Reputation: 3

Thanks this worked for me to have the last column saved as JSon using this:

df.assign(ln_A = lambda x: x.to_json()))

Upvotes: 0

jezrael
jezrael

Reputation: 862691

Use apply with parameter axis=1 for process by rows:

df.apply(lambda x: x.to_json("row{}.json".format(x.name)), axis=1)

If want only see output:

df.apply(lambda x: print(x.to_json()), axis=1)

EDIT:

Use custom function:

def f(x):
    a = x.to_json()
    print (a)


df.apply(f, axis=1)

Upvotes: 6

Yilun Zhang
Yilun Zhang

Reputation: 9018

.to_json(fname) function will write the data to a json file directly and will not return anything to you, so the print will return you none, but the actual files are there in your file system.


Update: if you want to save the json string as a variable in python, then don't specify the filename argument in the function to_json(). You can do: a = df.iloc[0].to_json() and the string will be saved to variable a.

Upvotes: 3

Related Questions