简体   繁体   中英

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

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))))
But when I then want to isolate all rows using uniques , I get an empty 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. But now I am out of ideas on how to incorporate the second part of the Multi-Index.

How can I iterate through my dataframe and extract all rows belonging to each glacier? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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