0

Having a jsong format like this:

{'reputation_history_type': 'post_upvoted', 'reputation_change': 10, 'post_id': 12, 'creation_date': 1217553646, 'user_id': 1}, {'reputation_history_type': 'post_downvoted', 'reputation_change': 10, 'post_id': 2, 'creation_date': 1217540009, 'user_id': 3}

How is it possible to convert it to a dataframe? example of expected output:

reputation_history_type reputation_change post_id creation_date user_id
Nathalie
  • 1,228
  • 7
  • 20
  • [`pd.read_json(file, orient='records')`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_json.html) – Lukas Thaler Dec 01 '19 at 13:02
  • @LukasThaler thank you. It is not a file it is a variable – Nathalie Dec 01 '19 at 13:06
  • Use `pd.DataFrame(d)` – jezrael Dec 01 '19 at 13:09
  • What format does the variable come in? `pd.read_json` works fine if it is a string. If the data comes as an array, convert it to string and read it after – Lukas Thaler Dec 01 '19 at 13:10
  • The string you provided is not json (JSON uses double quotes). Looks like a Python `dict` to me. Converting from the dict you stated: `import pandas as pd;_dict = {'reputation_history_type': 'post_upvoted', 'reputation_change': 10, 'post_id': 12, 'creation_date': 1217553646, 'user_id': 1}, {'reputation_history_type': 'post_downvoted', 'reputation_change': 10, 'post_id': 2, 'creation_date': 1217540009, 'user_id': 3}; pd.DataFrame(_dict)` – psychemedia Dec 01 '19 at 13:40

0 Answers0