简体   繁体   中英

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.

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)

.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 .

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.

感谢这对我有用,使用这个将最后一列保存为 JSon:

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

to produce a json record for each dataframe:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM