Ejemplo 1: Python convierte json a pandas dataframe
# Basic syntax:
dataframe = pd.DataFrame.from_dict(json_data, orient="index")
# Example usage:
import json
import pandas as pd
# Make json-formatted string:
json_string = '{ "name":"John", "age":30, "car":"None" }'
your_json = json.loads(json_string)
print(your_json)
--> {'name': 'John', 'age': 30, 'car': 'None'}
# Convert to pandas dataframe:
dataframe = pd.DataFrame.from_dict(your_json, orient="index")
print(dataframe)
0
name John
age 30
car None
# Note, orient="index" sets the keys as rownames. orient="columns" is
# the default and is supposed to set the keys as column names, but I
# couldn't seem to get it to work with this example
Ejemplo 2: pandas a json
>>> df.to_json(orient='records')
'[{"col 1":"a","col 2":"b"},{"col 1":"c","col 2":"d"}]'
Ejemplo 3: convertir dataframe a json
dictionary_varibale = [
dict([
(colname, row[i])
for i,colname in enumerate(df.columns)
])
for row in df.values
]
print(json.dumps(dictionary_varibale))
Ejemplo 4: marco de datos de json a pandas
json_to_pandas
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)