pandas 迭代熊猫系列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50267185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over pandas series
提问by Alan
I want to travel round the series index
我想绕系列索引
In [44]: type(ed1)
Out[44]: pandas.core.series.Series
In [43]: for _, row in ed1.iterrows():
...: print(row.name)
and I get thie error:
我得到了错误:
AtributeError: 'Series' ojbect has no attribute 'iterrows'
Is series has any methods like iterrows? thank a lot
系列有没有像 iterrows 这样的方法?非常感谢
回答by cs95
Series
objects define an iteritems
method (the data is returned as a iterator of index-value pairs.
Series
对象定义了一个iteritems
方法(数据作为索引值对的迭代器返回。
for _, val in ed1.iteritems():
...
Alternatively, you can iterate over a list by calling tolist
,
或者,您可以通过调用遍历列表tolist
,
for val in ed1.tolist():
...
Word of advice, iterating over pandas objects is generally discouraged. Wherever possible, seek to vectorize. To that end, I recommend taking a look at my answer to How to iterate over rows in a DataFrame in Pandas?which discusses better alternatives to iteration.
忠告,一般不鼓励迭代Pandas对象。在可能的情况下,寻求矢量化。为此,我建议查看我对如何在 Pandas 中的 DataFrame 中迭代行的回答?其中讨论了更好的迭代替代方案。