【发布时间】:2017-06-02 10:59:26
【问题描述】:
我有一个包含两列的列表,我想将其用作矩阵的行和列索引,另一列用作数据。我怎样才能像使用 csv 文件一样构建矩阵?
这是我的清单。我想将count 作为数据,eclipse_id 作为索引,最后一个作为列索引:
In[31]: listado
Out[31]:[{'count': 1L, 'eclipse_id': 10616, 'subscriber_id': 13},
{'count': 1L, 'eclipse_id': 10337, 'subscriber_id': 13},
{'count': 1L, 'eclipse_id': 9562, 'subscriber_id': 13},
{'count': 1L, 'eclipse_id': 10660, 'subscriber_id': 13},
{'count': 1L, 'eclipse_id': 10621, 'subscriber_id': 13},
我的尝试:
pd.DataFrame(data=listado[1:,0],
index=listado[2:,0]
columns=listado[3:,0])
With the error message :
File "<ipython-input-33-f87ac772eb69>", line 3
columns=listado[3:,0])
^
SyntaxError: invalid syntax
输出应该是这样的:
subscriber_id 13 14 15 16
eclipse_id
9562 1 1 0 ...
10337 1 0 0 ...
10616 1 2 0 ...
10621 1 1 1
10660 1 0 0
【问题讨论】:
-
在你的ctor参数
pd.DataFrame(data=listado[1:,0], index=listado[2:,0], #<- here columns=listado[3:,0])中缺少逗号,即使你添加了逗号,你也不能通过传递一个元组来分割列表,这就是使用[1:,0]会发生的事情 -
另外,您能否发布所需的输出,因为您的语法不正确,不清楚您真正想要实现的目标是什么
标签: python pandas matrix dataframe jupyter-notebook