简体   繁体   English

使用匹配的多索引值访问 pandas dataframe 的行

[英]Access rows of a pandas dataframe with matching Multiindex values

I have a pandas Dataframe with a Multi-Index (it can be accessed as a netcdf-file here ).我有一个带有多索引的pandas Dataframe(可以在此处作为 netcdf 文件访问)。 The data looks like the picture below - the Multi-Index ( names=['rgi_id', 'time'] ) is comprised of the glacier number (RGI60-15.00001 to RGI60-15.13000) and the point in time (2020 to 2101).数据如下图所示 - 多索引 ( names=['rgi_id', 'time'] ) 由冰川编号 (RGI60-15.00001 到 RGI60-15.13000) 和时间点 (2020 到 2101) 组成.

I want to extract all rows belonging to each glacier.我想提取属于每个冰川的所有行。 I've extracted the unique values for the first part of the Multiindex like so: uniques = np.unique(np.array(list(df.index.get_level_values(0))))我已经为 Multiindex 的第一部分提取了唯一值,如下所示: uniques = np.unique(np.array(list(df.index.get_level_values(0))))
But when I then want to isolate all rows using uniques , I get an empty dataframe:但是当我想使用uniques隔离所有行时,我得到一个空的 dataframe:

>>> df.loc[df.index == uniques[0]

Empty DataFrame
Columns: [hydro_year, hydro_month, calendar_year, calendar_month, volume, area, length, terminus_thick_0, terminus_thick_1, terminus_thick_2, water_level, glen_a, fs]
Index: []

I understand this is because uniques only consists of the first parts of the Multi-Index and therefore does not match.我理解这是因为uniques性仅包含多索引的第一部分,因此不匹配。 But now I am out of ideas on how to incorporate the second part of the Multi-Index.但是现在我对如何合并 Multi-Index 的第二部分一无所知。

How can I iterate through my dataframe and extract all rows belonging to each glacier?如何遍历我的 dataframe 并提取属于每个冰川的所有行? Am I on the right track or is there another solution?我在正确的轨道上还是有其他解决方案? I am aware of this solution, but I cannot apply it correctly...我知道这个解决方案,但我无法正确应用它......

在此处输入图像描述

Use df.loc[uniques[0]] to index on axis with labels in first level.使用df.loc[uniques[0]]在带有第一级标签的轴上进行索引。

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

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