Read .json file in pandas (python)
Question:
I am trying to read a .json file in pandas and couldn’t figure out how to. I was hoping if anyone can assist me with this.
My json file:
[[{"Auction_Day":31,"Auction_Month":1,"Auction_Year":2023,"Unit":"PQR-1","Provider":"PQR Energy","Volume":8.0,"Price":10.0,"EFA":2},{"Auction_Day":31,"Auction_Month":1,"Auction_Year":2023,"Unit":"PQR-2","Provider":"PQR Energy","Volume":14.0,"Price":71.0,"EFA":4},{"Auction_Day":1,"Auction_Month":2,"Auction_Year":2023,"Unit":"PQR-3","Provider":"PQR Energy","Volume":19.0,"Price":30.0,"EFA":6}]]
I am expecting something like this.
Also, I tried the read_json command but it does not work.
Thanks.
Answers:
It seems that your json is actually a list of dictionaries inside a list. So you should first extract the nested list (at the first position, i.e. at index 0) to convert it to a dataframe:
import pandas as pd
import json
with open("file.json", "r") as f:
data = json.load(f)
df = pd.DataFrame(data[0])
To read a .json file in pandas, you can use the read_json() function. Here is an example of how you can use it to read your specific .json file:
Note that the above code will only work if your json file is a valid json and it is in the format that pandas expect it to be. In your case, the json file is an array of arrays, containing a single array of objects. So, you need to pass the argument orient=’records’ when reading the json file to let pandas know that the json is an array of objects.
import pandas as pd
df = pd.read_json(‘path/to/your/file.json’,orient=’records’)
I am trying to read a .json file in pandas and couldn’t figure out how to. I was hoping if anyone can assist me with this.
My json file:
[[{"Auction_Day":31,"Auction_Month":1,"Auction_Year":2023,"Unit":"PQR-1","Provider":"PQR Energy","Volume":8.0,"Price":10.0,"EFA":2},{"Auction_Day":31,"Auction_Month":1,"Auction_Year":2023,"Unit":"PQR-2","Provider":"PQR Energy","Volume":14.0,"Price":71.0,"EFA":4},{"Auction_Day":1,"Auction_Month":2,"Auction_Year":2023,"Unit":"PQR-3","Provider":"PQR Energy","Volume":19.0,"Price":30.0,"EFA":6}]]
I am expecting something like this.
Also, I tried the read_json command but it does not work.
Thanks.
It seems that your json is actually a list of dictionaries inside a list. So you should first extract the nested list (at the first position, i.e. at index 0) to convert it to a dataframe:
import pandas as pd
import json
with open("file.json", "r") as f:
data = json.load(f)
df = pd.DataFrame(data[0])
To read a .json file in pandas, you can use the read_json() function. Here is an example of how you can use it to read your specific .json file:
Note that the above code will only work if your json file is a valid json and it is in the format that pandas expect it to be. In your case, the json file is an array of arrays, containing a single array of objects. So, you need to pass the argument orient=’records’ when reading the json file to let pandas know that the json is an array of objects.
import pandas as pd
df = pd.read_json(‘path/to/your/file.json’,orient=’records’)