传递值的形状为(2,3),索引意味着(2,1)

时间:2016-12-30 12:32:37

标签: python pandas

>>> pd.DataFrame({"a": [['*', 6.0689999999999884, 1, 0, [6.0689999999999884]], ['*', 6.0690000000000168, 1, 0, [6.0690000000000168]], ['*', 6.0689999999999884, 1,0, [6.0689999999999884]]], "b": 12}, index=[0, 1, 2])
                           a   b
0  [*, 6.069, 1, 0, [6.069]]  12
1  [*, 6.069, 1, 0, [6.069]]  12
2  [*, 6.069, 1, 0, [6.069]]  12

是的,创建了3行,但我想创建一行。

B:

我尝试以下方式,但它无法正常工作。

>>> pd.DataFrame({"a": [['*', 6.0689999999999884, 1, 0, [6.0689999999999884]], ['*', 6.0690000000000168, 1, 0, [6.0690000000000168]], ['*', 6.0689999999999884, 1,0, [6.0689999999999884]]], "b": 12}, index=[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/site-packages/pandas/core/frame.py", line 226, in __init__
    mgr = self._init_dict(data, index, columns, dtype=dtype)
  File "/usr/lib64/python2.6/site-packages/pandas/core/frame.py", line 363, in _init_dict
    dtype=dtype)
  File "/usr/lib64/python2.6/site-packages/pandas/core/frame.py", line 5168, in _arrays_to_mgr
    return create_block_manager_from_arrays(arrays, arr_names, axes)
  File "/usr/lib64/python2.6/site-packages/pandas/core/internals.py", line 3916, in create_block_manager_from_arrays
    construction_error(len(arrays), arrays[0].shape, axes, e)
  File "/usr/lib64/python2.6/site-packages/pandas/core/internals.py", line 3882, in construction_error
    passed,implied))
ValueError: Shape of passed values is (2, 3), indices imply (2, 1)

C

通过创建列表列表以下列方式工作。

>>> pd.DataFrame({"a": [[['*', 6.0689999999999884, 1, 0, [6.0689999999999884]], ['*', 6.0690000000000168, 1, 0, [6.0690000000000168]], ['*', 6.0689999999999884, 1,0, [6.0689999999999884]]]], "b": 12}, index=[0])
                                                   a   b
0  [[*, 6.069, 1, 0, [6.069]], [*, 6.069, 1, 0, [...  12
>>> 

但是我的输入就像上面的B。

那该怎么办?

2 个答案:

答案 0 :(得分:4)

如果 C 正在运作,那么,为什么你没有使用 C ? 当你说'我的输入如上面B&#39;你的意思是你有一个变量,比方说,数据,如下:

data = [['*', 6.0689999999999884, 1, 0, [6.0689999999999884]], ['*', 6.0690000000000168, 1, 0, [6.0690000000000168]], ['*', 6.0689999999999884, 1,0, [6.0689999999999884]]]

然后,为什么不简单地使用它:

pd.DataFrame({"a": [data], "b": 12}, index=[0])

我不确定我是否完全理解你想要的东西。

答案 1 :(得分:1)

您可以在创建后将数据框转换为一行:

df = pd.DataFrame({"a": [['*', 6.0689999999999884, 1, 0, [6.0689999999999884]], ['*', 6.0690000000000168, 1, 0, [6.0690000000000168]], ['*', 6.0689999999999884, 1,0, [6.0689999999999884]]], "b": 12}, index=[0, 1, 2])

df.groupby('b').a.apply(list).reset_index()

enter image description here