简体   繁体   English

多索引数据框中的Python Pandas引用值

[英]Python Pandas Referencing Value in Multiindex Dataframe

I have a multi-index dataframe with 1 column of data as follows: 我有一个包含1列数据的多索引数据框,如下所示:

                                  Weight
Ticker       date
ABC    2017-03-31 00:00:00        .00292
  ...       ...                       ...

I am referencing it as follows: 我引用它如下:

df.loc[(ticker,date),'Weight']

Instead of getting a value returned I'm getting this: 我没有得到返回的值,而是得到了:

Ticker     date
ABC       2017-03-31     0.00292

instead of this: 代替这个:

0.00292

When I type info() it tells me I have a MultiIndex Dataframe with 1 data column. 当我键入info()它告诉我我有一个包含1个数据列的MultiIndex Dataframe。

I have data/code like this all throughout a script - but with this particular data (which looks clean) and this syntax it isn't working. 我在整个脚本中都有这样的数据/代码-但是使用此特定数据(看起来很干净)并且使用此语法无法正常工作。

Strangely, when I do this: 奇怪的是,当我这样做时:

df.loc[(ticker,date),'Weight'][0]

it returns 它返回

0.00292

But when I try to iterate through the df it seems to revert to 但是,当我尝试遍历df时,它似乎恢复为

Ticker     date
ABC       2017-03-31     0.00292

And then I can't perform other operations. 然后我无法执行其他操作。

Any idea what's going on? 知道发生了什么吗?

Absolutely crazy. 绝对疯狂。 Finally resolved by just using [0] at the end as noted above. 最后,仅需在末尾使用[0]即可解决。 All available information points to the data in a regular dataframe that is correctly changed to a multi indexed dataframe. 所有可用信息均指向常规数据框中的数据,该数据已正确更改为多索引数据框。 No data irregularities either. 也没有数据异常。 Very weird 很奇怪

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

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