One - One Code All

Blog Content

python中字典转dataframe报错If using all scalar values, you must pass an index

Python   2011-06-20 20:05:27

python中dataframe报错If using all scalar values, you must pass an index

如果处理的对象是字典,在将dict转为DataFrame时,报这个错误,:If using all scalar values, you must pass an index

例如:summary = pd.DataFrame({key:value for key,value in test.items()if key in index}) #查看汇总表情况

【解决办法】

添加参数:index = [0]

summary = pd.DataFrame({key:value for key,value in test.items()if key in index},index = [0]) #查看汇总表情况
没报错!问题解决!


如果处理的对象是json, pd.read_json(data_file)  访问报错,可添加 typ='series',pd.read_json(data_file,typ='series') 或者使用with open 先转换成字典再转换成pd.dataframe   


上一篇:python中Nonetype与空值
下一篇:python读取json格式为DataFrame(可转为.csv)

The minute you think of giving up, think of the reason why you held on so long.