Iterate over pandas series

后端 未结 1 1463
闹比i
闹比i 2021-01-11 23:03

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.n         


        
相关标签:
1条回答
  • 2021-01-11 23:53

    Series objects define an iteritems method (the data is returned as a iterator of index-value pairs.

    for _, val in ed1.iteritems():
        ...
    

    Alternatively, you can iterate over a list by calling 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.

    0 讨论(0)
提交回复
热议问题