简体   繁体   English

如何返回熊猫数据框中元素的索引值

[英]How to return the index value of an element in a pandas dataframe

I have a dataframe of corporate actions for a specific equity. 我有一个针对特定股权的公司行为数据框架。 it looks something like this: 它看起来像这样:

0             Declared Date       Ex-Date    Record Date
BAR_DATE                 
2018-01-17       2017-02-21    2017-08-09     2017-08-11
2018-01-16       2017-02-21    2017-05-10     2017-06-05

except that it has hundreds of rows, but that is unimportant. 除了它有数百行,但这并不重要。 I created the index "BAR_DATE" from one of the columns which is where the 0 comes from above BAR_DATE. 我从其中一列从0开始于BAR_DATE的列中创建了索引“ BAR_DATE”。

What I want to do is to be able to reference a specific element of the dataframe and return the index value, or BAR_DATE, I think it would go something like this: 我想做的是能够引用数据帧的特定元素并返回索引值或BAR_DATE,我认为它会像这样:

index_value = cacs.iloc[5, :].index.get_values()

except index_value becomes the column names, not the index. 除了index_value成为列名,而不是索引。 Now, this may stem from a poor understanding of indexing in pandas dataframes, so this may or may not be really easy to solve for someone else. 现在,这可能是由于对熊猫数据帧中的索引了解不足,所以对于其他人而言,这可能很难解决。

I have looked at a number of other questions including this one , but it returns column values as well. 我看了其他一些问题,包括这个问题,但它也返回列值。

Your code is really close, but you took it just one step further than you needed to. 您的代码确实很接近,但是只比您需要的多了一步。

# creates a slice of the dataframe where the row is at iloc 5 (row number 5) and where the slice includes all columns
slice_of_df = cacs.iloc[5, :]

# returns the index of the slice
# this will be an Index() object
index_of_slice = slice_of_df.index

From here we can use the documentation on the Index object: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Index.html 从这里我们可以使用Index对象的文档: https : //pandas.pydata.org/pandas-docs/stable/generation/pandas.Index.html

# turns the index into a list of values in the index
index_list = index_of_slice.to_list()

# gets the first index value
first_value = index_list[0]

The most important thing to remember about the Index is that it is an object of its own, and thus we need to change it to the type we expect to work with if we want something other than an index. 关于Index ,要记住的最重要的一点是它是它自己的对象,因此,如果我们需要索引以外的其他东西,我们需要将其更改为我们期望使用的类型。 This is where documentation can be a huge help. 这是文档可以提供巨大帮助的地方。

EDIT: It turns out that the iloc in this case is returning a Series object which is why the solution is returning the wrong value. 编辑:事实证明,在这种情况下,iloc返回的是Series对象,这就是为什么解决方案返回错误值的原因。 Knowing this, the new solution would be: 知道这一点,新的解决方案将是:

# creates a Series object from row 5 (technically the 6th row)
row_as_series = cacs.iloc[5, :]

# the name of a series relates to it's index
index_of_series = row_as_series.name

This would be the approach for single-row indexing. 这将是单行索引的方法。 You would use the former approach with multi-row indexing where the return value is a DataFrame and not a Series . 您将对多行索引使用前一种方法,其中返回值是DataFrame而不是Series

Unfortunately, I don't know how to coerce the Series into a DataFrame for single-row slicingbeyond explicit conversion: 不幸的是,除了显式转换之外,我不知道如何将Series强制为DataFrame进行单行切片:

row_as_df = DataFrame(cacs.iloc[5, :])

While this will work, and the first approach will happily take this and return the index, there is likely a reason why Pandas doesn't return a DataFrame for single-row slicing so I am hesitant to offer this as a solution. 虽然这将起作用,并且第一种方法将很乐意采用该方法并返回索引,但是Pandas可能没有为单行切片不返回DataFrame的原因可能是一个原因,因此我很犹豫地提供此解决方案。

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

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