Saving Python dictionaries using json
What is json? JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript
object syntax but applicable to other platforms. It is commonly used for transmitting data in various applications (e.g., sending some data
from the server to the client or vice versa).
using json
In [ ]: import numpy as np
from [Link] import solve_ivp
import [Link] as plt
import pickle
import json
In [ ]: # Load this file from last class -- attached with code
new_dict = [Link]('[Link]',allow_pickle = True).item(0)
new_dict
In [ ]: with open("[Link]", "w") as outfile: # Open a new json file, for writing "w"
[Link](new_dict,outfile,indent=4) # Dump the new_dict dictionary to the open json file
In [ ]: new_dict
In [ ]: # [Link] are not "json" serializable
new_dict["Century"]= new_dict["Century"].tolist() # Convert it into a list
print (new_dict)
In [ ]: with open("[Link]", "w") as outfile: # Open a new json file, for writing "w"
[Link](new_dict,outfile,indent=4) # Dump the new_dict dictionary to the open json file
In [ ]: loadfile = open("[Link]") # Open a saved json file
json_dict = [Link](loadfile) # Dump the opened a file to a variable ('dictionary')
json_dict
In [ ]: with open("[Link]") as loadfile: # Open a saved json file
json_dict = [Link](loadfile) # Dump the opened a file to a variable ('dictionary')
json_dict
In [ ]: json_dict["City"] = ['Bengaluru','Kolkata','Mumbai']
In [ ]: # json_dict
with open("[Link]","w") as loadfile:
[Link](json_dict,loadfile,indent=4)
In [ ]: # Write a function that automatically adds data to your json file
# def add_jsonfile(add_key,add_value,filename='[Link]'):
# with open(filename, "r+") as outfile: # Open a json file, for reading and writing "r+"
# dict_ = [Link](outfile)
# dict_[add_key] = add_value
# with open(filename, "w") as outfile: # Open the json file, only for writing "w" to overwrite the file
# [Link](dict_,outfile,indent=4)
# add_jsonfile('City',['Bengaluru','Kolkata','Mumbai','Hyderabad'],'[Link]')
Second json file
In [ ]: students = {
"2022": [{'Name':'Ajay',
'Age':18,
'Roll':'22ND075'},
{'Name':'Vijay',
'Age':19,
'Roll':'22ND867'},
{'Name':'Tanuj',
'Age':19,
'Roll':'22ND105'}
]
}
In [ ]: with open("[Link]", "w") as outfile: # Create a new json file "w"
[Link](students,outfile,indent=2)
In [ ]: students['2021']=[]
print (students)
with open("[Link]", "w") as outfile: # If I want to overwrite then always use "w"
[Link](students,outfile,indent=2)
In [ ]: def append_jsonfile(add_key,add_value,filename='[Link]'):
with open(filename, "r+") as outfile: # Open the json file, for reading and writing "r+"
dict_ = [Link](outfile)
dict_[add_key].append(add_value)
with open(filename, "w") as outfile:
[Link](dict_,outfile,indent=2)
In [ ]: add_val = {'Name':'Alex',
'Age': 21,
'Roll':'21ND005'}
append_jsonfile('2021',add_val,'[Link]')
In [ ]: add_jsonfile('2020',[],'[Link]')
In [ ]: with open("[Link]") as outfile: # If I want to overwrite then always use "w"
new_student = [Link](outfile)
In [ ]: new_student
Using pandas
Pandas is a Python library that provides various data structures and operations for manipulating numerical data. Built on top of the NumPy
library, Pandas is fast, productive and high performing.
[Link]
In [ ]: import pandas as pd
list0 = (students['2022'][1])
print (list0,'\n')
print("This is a Panda series,\n")
A = [Link](list0) # Converting the above list to a Panda series
print(A)
In [ ]: list_2022 = [Link](students['2022']) # Converting our dictionary object to a Panda Dataframe
print("This is a Panda Dataframe,\n")
print (list_2022)
In [ ]: print(list_2022[['Name','Age']])
In [ ]: large_data = pd.read_csv("ipl_data.csv") # Loading a large data set
In [ ]: large_data.keys()
In [ ]: print (large_data['city'])
In [ ]: print (large_data.loc[list(range(20,25)),['toss_winner','winner']])
In [ ]: count = 0
for i,j in large_data.iterrows():
if j['city'] == 'Kolkata':
print(j['id'],j['winner'])
count += 1
print()
print ("Games held in Kolkata: ",count)
In [ ]: large_data = pd.read_csv("ipl_data.csv",index_col='season') # Loading a large data set with a fixed index
In [ ]: # print (large_data.loc[[2008],['winner','player_of_match']])
Tasks for today
Solve the following problems.
Create a dictionary containing the name of your five of your friends, their city of birth, hometown, and a fictional passport number.
Save the above dictionary a json file and create a function, that can upload the file and add a new friend's information, and then save the
json file. Add the Head as the new friend, {'SDhar','Kolkata','Mumbai','XYZ789'}. Upload the file again and display the dictionary as a
dataframe using pandas.
Upload the file "ipl_data.csv" and count the number of matches won by "Chennai Super Kings" in each season.
From the above file "ipl_data.csv" and count the number of matches where the umpire was 'DJ Harper'.
In [ ]: