0

How do I open a json file and turn it into a dataframe? I want to create graphs with Plotly later but I cannot seem to create a dataframe.

def open_file(filename):
  with open(filename, "r") as file:
    file_list = json.load(file)
    output_df = pd.DataFrame(file_list)
    return output_df


open_file("LT.json")

The error I get:

ValueError("DataFrame constructor not properly called!")

2
  • 3
    Use pandas.read_json. Commented Oct 16, 2021 at 16:24
  • share the json and explain how the df should look like. Commented Oct 16, 2021 at 18:27

1 Answer 1

1

The error ValueError("DataFrame constructor not properly called!") is telling you that you passed the incorrect type of data to the DataFrame. So, most probably file_list is not a dictionary. In my case, your code is 100% working. So, I think the problem is definitely from the JSON file. Maybe file_list is a type of string. If that is the case, using the eval method might help.

output_df = pd.DataFrame(eval(file_list))

But using pandas.read_json works 99% of the time

output_df = pd.read_json(filename)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.