pythonpandasto-json

How to create json from a list of dataframes in a for loop?


I have a list with multiple dataframes in it. I want create json for each df in list using for loop.

I tried,

dfsize=[df df df]

for dfs in dfsize:
    dfs.to_json('areca{dfs}.json').format(dfs)

Was met with the error:

AttributeError: 'NoneType' object has no attribute 'format'

Is there a way to achieve this?

Also tried:

I tried

dfs.to_json('areca{dfs}.json'.format(dfs))

but got the error:

dfs.to_json('areca{dfs}.json'.format(dfs.index))

KeyError: 'dfs'


Solution

  • pandas.DataFrame.to_json returns None. This is causing the error. Format should be inside. Try this,

    dfsize=[df df df]
    
    for dfs in dfsize:
        dfs.to_json('areca{dfs}.json'.format(dfs))