遍历大熊猫系列

Ala*_*lan 8 python series pandas

我想绕过系列索引

In [44]: type(ed1)
Out[44]: pandas.core.series.Series

In [43]: for _, row  in ed1.iterrows():
...:     print(row.name)
Run Code Online (Sandbox Code Playgroud)

我得到错误:

  AtributeError: 'Series' ojbect has no attribute 'iterrows'
Run Code Online (Sandbox Code Playgroud)

系列是否有类似迭代的方法?非常感谢

cs9*_*s95 11

Series对象定义一个iteritems方法(数据作为索引值对的迭代器返回。

for _, val in ed1.iteritems():
    ...
Run Code Online (Sandbox Code Playgroud)

或者,您可以通过调用来遍历列表tolist

for val in ed1.tolist():
    ...
Run Code Online (Sandbox Code Playgroud)

通常不建议在熊猫对象上进行迭代。尽可能寻求矢量化。为此,我建议看一下我的答案如何在Pandas中的DataFrame中的行上进行迭代?讨论了迭代的更好替代方案。