Read Json with NaN into Python and Pandas -
i understand nan not allowed in json files. use
import pandas pd pd.read_json('file.json')
to read in json python. looking through documentation, not see option handle value.
i have json file, data.json, looks
[{"city": "los angeles","job":"chef","age":30}, {"city": "new york","job":"driver","age":35}, {"city": "san jose","job":"pilot","age":nan}]
how can read python/pandas , handle nan values?
edit:
amazing answer below!! fixxxer!! it's documented, reading in separate file
import pandas pd import json text=open('data.json','r') x=text.read() y=json.loads(x) data=pd.dataframe(y) data.head()
read json file variable:
x = '''[{"city": "los angeles","job":"chef","age":30}, {"city": "new york","job":"driver","age":35}, {"city": "san jose","job":"pilot","age":nan}]'''
now, load json.loads
in [41]: import json in [42]: y = json.loads(x) in [43]: y out[43]: [{u'age': 30, u'city': u'los angeles', u'job': u'chef'}, {u'age': 35, u'city': u'new york', u'job': u'driver'}, {u'age': nan, u'city': u'san jose', u'job': u'pilot'}]
and,
in [44]: pd.dataframe(y) out[44]: age city job 0 30 los angeles chef 1 35 new york driver 2 nan san jose pilot
Comments
Post a Comment