简体   繁体   English

从Pandas中的iterrows()获取行位置而不是行索引

[英]Get Row Position instead of Row Index from iterrows() in Pandas

I'm new to stackoverflow and I have research but have not find a satisfying answer. 我是stackoverflow的新手,我有研究但没有找到令人满意的答案。

I understand that I can get a row index by using df.iterrows() to iterate through a df. 我知道我可以通过使用df.iterrows()来迭代df来获得行索引。 But what if I want to get a row position instead of row idx. 但是,如果我想获得行位置而不是行idx,该怎么办? What method can I use? 我可以使用什么方法?

Example code that I'm working on is below: 我正在处理的示例代码如下:

df = pd.DataFrame({'month': ['Jan', 'Feb', 'March', 'April'],
               'year': [2012, 2014, 2013, 2014],
               'sale':[55, 40, 84, 31]})

df = df.set_index('month')

for idx, value in df.iterrows():
    print(idx)

How can I get an output of: 如何获得输出:

0
1
2
3

Thanks! 谢谢!

Simply use enumerate : 只需使用enumerate

for idx, (_, value) in enumerate(df.iterrows()):
    print(idx)

If you need row number instead of index, you should: 如果您需要行号而不是索引,则应该:

  1. Use enumerate for a counter within a loop. 对循环中的计数器使用enumerate
  2. Don't extract the index, see options below. 不要提取索引,请参阅下面的选项。

Option 1 选项1

In most situations, for performance reasons you should try and use df.itertuples instead of df.iterrows . 在大多数情况下,出于性能原因,您应该尝试使用df.itertuples而不是df.iterrows You can specify index=False so that the first element is not the index. 您可以指定index=False以便第一个元素不是索引。

for idx, row in enumerate(df.itertuples(index=False)):
    # do something

df.itertuples returns a namedtuple for each row. df.itertuples为每一行返回一个df.itertuples

Option 2 选项2

Use df.iterrows . 使用df.iterrows This is more cumbersome, as you need to separate out an unused variable. 这更麻烦,因为您需要分离出一个未使用的变量。 In addition, this is inefficient vs itertuples . 另外,这对itertuples来说效率itertuples

for idx, (_, row) in enumerate(df.iterrows()):
    # do something

You can use get_loc on df.index: 你可以在get_loc上使用get_loc:

for idx, value in df.iterrows():
    print(idx, df.index.get_loc(idx))

Output: 输出:

Jan 0
Feb 1
March 2
April 3

You can use df.index() which returns a range of indexes numbers. 您可以使用df.index()返回一系列索引号。 The returned value is a RangeIndex object which is a range like iterable that supports iteration and many other functionalities that a Pandas series supports : 返回的值是一个RangeIndex对象,它是一个像iterable一样的range ,支持迭代和Pandas系列支持的许多其他功能:

>>> df.index
RangeIndex(start=0, stop=4, step=1)
>>> 
>>> list(df.index)
[0, 1, 2, 3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM