DataFrame与Json转换
- DataFrame转Json
- 将Json转为DataFrame
- str,unicode格式的list转list
DataFrame转Json
import pandas as pd
df = pd.read_csv("./PoliceKillingsUS.csv").ix[0:10,2:-1]
df_to_json = df.to_json(orient="records")
df:
df_to_json:
将Json转为DataFrame
import pandas as pd
import json
from pandas.io.json import json_normalize
str_to_list = json.loads(df_to_json)
df0 = pd.DataFrame(str_to_list)
df1 = json_normalize(str_to_list)
df0:
df1:
str,unicode格式的list转list
import pandas as pd
import json
import ast
from pandas.io.json import json_normalize
df = pd.read_csv("./PoliceKillingsUS.csv").ix[0:10,2:-1]
df_to_json = json.dumps(df.to_json(orient="records").replace(':true',":True").replace('false',"False"))
json_to_str = json.loads(df_to_json).encode('utf-8')
json_to_list = ast.literal_eval(json_to_str)
import pandas as pd
import json
import ast
from pandas.io.json import json_normalize
df = pd.read_csv("./PoliceKillingsUS.csv").ix[0:10,2:-1]
df_to_json = json.dumps(df.to_json(orient="records").replace(':true',":True").replace('false',"False"))
json_to_str = json.loads(df_to_json)
json_to_list = ast.literal_eval(json_to_str)