Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dump Pandas Dataframe to multiple json files

Tags:

json

pandas

I have a Dataframe loaded from a CSV file

df = pd.read_csv(input_file, header=0)

and I want to process it and eventually save it into multiple JSON files (for example a new file every X rows).

Any suggestion how to achieve that?

like image 582
Beppe C Avatar asked Oct 19 '25 12:10

Beppe C


1 Answers

This should work:

for idx, group in df.groupby(np.arange(len(df))//10): 
    group.to_json(f'{idx}_name.json', orient='index') # orient: split, records, index, values, table, columns

Change the 10 to the number of rows you wish to write out for each iteration.

like image 91
Ian Avatar answered Oct 22 '25 04:10

Ian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!