ValueError: If using all scalar values, you must pass an index

刘超 1月前 ⋅ 239 阅读   编辑

一、描述
    使用pandas构建dataframe报如下错误

>>> import pandas as pd
>>> df2 = pd.DataFrame({'A':1,'B':2})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 348, in __init__
    mgr = self._init_dict(data, index, columns, dtype=dtype)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 459, in _init_dict
    return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 7356, in _arrays_to_mgr
    index = extract_index(arrays)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 7393, in extract_index
    raise ValueError('If using all scalar values, you must pass'
ValueError: If using all scalar values, you must pass an index

二、解决方法

# 使用列表构建
df = pd.DataFrame({'A': [1], 'B': [2]})
# 使用标量与索引构建
df = pd.DataFrame({'A': 1, 'B': 2}, index=[0])

注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: