pandas dataframe to json code example
Example 1: python convert json to pandas dataframe
dataframe = pd.DataFrame.from_dict(json_data, orient="index")
import json
import pandas as pd
json_string = '{ "name":"John", "age":30, "car":"None" }'
your_json = json.loads(json_string)
print(your_json)
--> {'name': 'John', 'age': 30, 'car': 'None'}
dataframe = pd.DataFrame.from_dict(your_json, orient="index")
print(dataframe)
0
name John
age 30
car None
Example 2: pandas to json
>>> df.to_json(orient='records')
'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'
Example 3: convert dataframe to json
dictionary_varibale = [
dict([
(colname, row[i])
for i,colname in enumerate(df.columns)
])
for row in df.values
]
print(json.dumps(dictionary_varibale))
Example 4: json to pandas dataframe
json_to_pandas