在python中将数据分配给数据框时出错

时间:2020-04-28 12:52:00

标签: python pandas data-science linear-regression

线性回归对象的系数值如下所示

lm.coef_

array([[1.03378029e-14, 1.04028679e-15, 8.07704983e-19, 4.56541560e-17,
        2.55053327e-21, 1.37639192e-19, 7.00090443e-20, 4.49333292e-18,
        4.69773915e-19, 1.40607083e-17, 8.98242406e-11, 2.24738507e-14,
        1.15387240e-23, 1.47348953e-21, 5.88673992e-20, 1.51362786e-20,
        9.07331001e-22, 4.82239779e-21, 5.32335970e-21, 7.69150403e-20,
        8.85261229e-21, 5.27657684e-21]])

当我尝试使用以下命令排列数据并将其放入数据框之一时,它给我错误

**cdf = pd.DataFrame(lm.coef_,X.columns,columns=['Coeff'])**

Traceback (most recent call last):
  File "C:\<path>\Python37\lib\site-packages\pandas\core\internals.py", line 4857, in create_block_manager_from_blocks
    placement=slice(0, len(axes[0])))]
  File "C:\<path>\Python37\lib\site-packages\pandas\core\internals.py", line 3205, in make_block
    return klass(values, ndim=ndim, placement=placement)
  File "C:\<path>\Python37\lib\site-packages\pandas\core\internals.py", line 125, in __init__
    '{mgr}'.format(val=len(self.values), mgr=len(self.mgr_locs)))
**ValueError: Wrong number of items passed 22, placement implies 1**

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\<path>\Python37\lib\site-packages\pandas\core\frame.py", line 379, in __init__
    copy=copy)
  File "C:\<path>\Python37\lib\site-packages\pandas\core\frame.py", line 536, in _init_ndarray
    return create_block_manager_from_blocks([values], [columns, index])
  File "C:\<path>\Python37\lib\site-packages\pandas\core\internals.py", line 4866, in create_block_manager_from_blocks
    construction_error(tot_items, blocks[0].shape[1:], axes, e)
  File "C:\<path>\Python37\lib\site-packages\pandas\core\internals.py", line 4843, in construction_error
    passed, implied))
**ValueError: Shape of passed values is (22, 1), indices imply (1, 22)**

请帮助我输入正确的代码。

0 个答案:

没有答案