January 2019
Intermediate to advanced
390 pages
9h 16m
English
JSON strings or files can be read with the pandas.read_json() function, which returns a DataFrame or series object. For example, the following code reads the zips.json file:
df = pd.read_json(os.path.join(data_folder,data_file), lines=True)print(df)
We set lines=True because each line contains a separate object in JSON format. Without this argument being set to True, pandas will raise ValueError. The DataFrame is printed as follows:
_id city loc pop state 0 1001 AGAWAM [-72.622739, 42.070206] 15338 MA 1 1002 CUSHMAN [-72.51565, 42.377017] 36963 MA ... ... ... ... ... ... 29351 99929 WRANGELL [-132.352918, 56.433524] 2573 AK 29352 99950 KETCHIKAN [-133.18479, 55.942471] 422 AK [29353 rows x 5 columns]
Read now
Unlock full access