Skip to content Skip to sidebar Skip to footer

Json To Dataframe Conversion Issues

{'endDate': '2017-12-31', 'results': [{'data': [{'period': '2017-01-01', 'ratio': 26.91301}, {'period': '2017-02-01', 'ratio': 19.77063}, {'period': '2017-03-01', 'ratio':

Solution 1:

There is possible check length by len(r['data']) > 0 and filter out empty DataFrames in list comprehension:

lst= [pd.DataFrame(r['data']).set_index('period').rename(columns={'ratio':r['title']})forrind['results'] iflen(r['data'])>0]
df=pd.concat(lst,1)print(df)Data_specDate_rateData_underData_tuneperiod2017-01-01   26.9130117.6513979.176440.704172017-02-01   19.7706314.5261884.018511.119972017-03-01   20.4077515.00234100.000001.810742017-04-01   16.0284312.1521091.194421.388232017-05-01   10.3815910.6364493.219760.979142017-06-01    8.208708.5976793.420961.140092017-07-01    8.678158.9531289.148950.784652017-08-01   19.5895613.0574791.851651.079732017-09-01   36.9458748.0048291.241360.945612017-10-01   36.2819423.7811090.356110.851722017-11-01   16.6454316.9002781.885851.274222017-12-01    1.676610.898667.491110.08718

EDIT:

There si possible create custom DataFrame if r['data'] are empty, for align index is used d['startDate']:

lst = [pd.DataFrame(r['data']).set_index('period').rename(columns={'ratio' : r['title']}) 
        iflen(r['data']) > 0else pd.DataFrame([np.nan], columns=[r['title']], index=[d['startDate']])
        for r in d['results'] ]
df = pd.concat(lst, 1)
print (df)
            Data_spec  Date_rate  Data_over  Data_under  Data_tune
2017-01-0126.9130117.65139        NaN    79.176440.704172017-02-0119.7706314.52618        NaN    84.018511.119972017-03-0120.4077515.00234        NaN   100.000001.810742017-04-0116.0284312.15210        NaN    91.194421.388232017-05-0110.3815910.63644        NaN    93.219760.979142017-06-018.208708.59767        NaN    93.420961.140092017-07-018.678158.95312        NaN    89.148950.784652017-08-0119.5895613.05747        NaN    91.851651.079732017-09-0136.9458748.00482        NaN    91.241360.945612017-10-0136.2819423.78110        NaN    90.356110.851722017-11-0116.6454316.90027        NaN    81.885851.274222017-12-011.676610.89866        NaN     7.491110.08718

Post a Comment for "Json To Dataframe Conversion Issues"