1

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

enter image description here

2
  • I can't access it; it has the extension .nc and is not a text file Commented May 13, 2021 at 9:44
  • Yeah, sorry. I forgot to mention it in the question - the data is a netcdf file which I transformed into a pandas dataframe. Commented May 13, 2021 at 9:53

1 Answer 1

0

Use df.loc[uniques[0]] to index on axis with labels in first level.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.