PyPackage01---Pandas09_从字典创建dataframe
Intro
很简单的需求,不多说,直接看代码
import pandas as
pd.__version__
'0.23.4'
Demo
dict1 = {"x1": 1, "x2": 2, "x3": 3}pd.DataFrame(dict1)
---------------------------------------------------------------------------ValueError Traceback (most recent call last) in () 1 dict1 = {"x1": 1, "x2": 2, "x3": 3}----> 2 pd.DataFrame(dict1)D:\code\anaconda\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy) 346 dtype=dtype, copy=copy) 347 elif isinstance(data, dict):--> 348 mgr = self._init_dict(data, index, columns, dtype=dtype) 349 elif isinstance(data, ma.MaskedArray): 350 import numpy.ma.mrecords as mrecordsD:\code\anaconda\lib\site-packages\pandas\core\frame.py in _init_dict(self, data, index, columns, dtype) 457 arrays = [data[k] for k in keys] 458 --> 459 return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype) 460 461 def _init_ndarray(self, values, index, columns, dtype=None, copy=False):D:\code\anaconda\lib\site-packages\pandas\core\frame.py in _arrays_to_mgr(arrays, arr_names, index, columns, dtype) 7354 # figure out the index, if necessary 7355 if index is None:-> 7356 index = extract_index(arrays) 7357 7358 # don't force copy because getting jammed in an ndarray anywayD:\code\anaconda\lib\site-packages\pandas\core\frame.py in extract_index(data) 7391 7392 if not indexes and not raw_lengths:-> 7393 raise ValueError('If using all scalar values, you must pass' 7394 ' an index') 7395 ValueError: If using all scalar values, you must pass an index
卡卡报了一堆错,这个错误可以通过修改dict解决,即value改成list
dict2 = {"x1": [1], "x2": [2], "x3": [3]}pd.DataFrame(dict2)
如果不修改原始数据咋整
dict1 = {"x1": 1, "x2": 2, "x3": 3}pd.DataFrame([dict1])
Ref
[1] 于南京市江宁区九龙湖
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~