Python pandas系列通过索引: Data must be 1 dimensional error访问

标签 python pandas dataframe series

我有一个 pandas 系列(名为“clusters”),它看起来像:

0 [[1, 2, 3], [4, 5, 6]]
1 [[1, 2, 3], [9, 10, 11]]

我通过转换得到这个系列:list > dataframe > as_matrix 处理矩阵后我得到了系列。

我想通过索引(这里是 0 和 1)访问该系列。 但是当我做集群[0]或集群[1]时。

我收到错误数据必须是一维错误

我不知道问题出在哪里。

或者,如果循环浏览本系列,我如何访问索引? 所以如果我说:

for k in clusters:
      print k

我得到[[1,2,3],[4,5,6]]。但我想获取“[[1, 2, 3], [4, 5, 6]]”链接到的索引。我怎样才能得到它。我尝试了 k.index 但没有任何效果。

最佳答案

您可以迭代项目,它使用索引标签进行迭代:

In [11]: for ind, k in clusters.items():
             print(ind)
0
1

我认为您的系列有一些有趣的地方,因为您应该能够通过索引访问:

In [12]: clusters[0]
Out[12]: [[1, 2, 3], [4, 5, 6]]

In [13]: clusters.loc[0]
Out[13]: [[1, 2, 3], [4, 5, 6]]

关于Python pandas系列通过索引: Data must be 1 dimensional error访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33134321/

相关文章:

python - 校验和值与名为 'isRecyclableGarden' 的资源不匹配

python - 是否有 pandas 函数来计算特定单词之后出现的元素?

python - 使用循环或列表理解创建多个 pandas 数据框

r - 转置数据框

原生数据容器与 Pandas DataFrame 的 Python 性能对比

python - 在这个 python 代码中产生 "TypeError character mapping must return integer..."是什么?

python - 从文件 : how would you make this better? 中找到 N 最大的行

python - 根据列使用函数满足的条件在 Pandas 中创建新列

python - 如何使 x 和 y 轴标签的文本大小以及 matplotlib 和 prettyplotlib 图形上的标题更大

python - 尝试使用 Deque 来限制传入数据的 DataFrame ......建议?