Getting the last element of a level in a multiindex

2024/10/9 4:25:24

I have a dataframe in this format:

a   b   x
1   1   31
1   2   1
1   3   42
1   4   423
1   5   42
1   6   3
1   7   44
1   8   65437
1   9   73
2   1   5656
2   2   7
2   3   5
2   4   5
2   5   34

a and b are indexes, x is the value.

I want to get rows 1 9 73 and 2 5 34, in other words, the last row of that level.

I've been messing with .loc, .iloc, and .xs for an hour, but I can't get it to work. How do I do this?

Answer

You can use groupby with last:

print (df.groupby('a', as_index=False).last())a  b   x
0  1  9  73
1  2  5  34

If a and b are levels of MultiIndex, first call reset_index:

print (df.reset_index().groupby('a', as_index=False).last())a  b   x
0  1  9  73
1  2  5  34
https://en.xdnf.cn/q/70055.html

Related Q&A

Sphinx and JavaScript Documentation Workflow [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Getting two characters from string in python [duplicate]

This question already has answers here:Split string every nth character(19 answers)How to iterate over a list in chunks(40 answers)Closed last year.how to get in python from string not one character, b…

I Call API from PYTHON I get the response 406 Not Acceptable

I created a API in my site and Im trying to call an API from python but I always get 406 as a response, however, if I put the url in the browser with the parameters, I can see the correct answerI alrea…

TypeError: unsupported operand type(s) for +=: builtin_function_or_method and int

I am receiving this error (TypeError: unsupported operand type(s) for +=: builtin_function_or_method and int) when trying to run this codetotal_exams = 0 for total_exams in range(1, 100001):sum += tota…

Project Scipy Voronoi diagram from 3d to 2d

I am trying to find a way to calculate a 2d Power Diagram in Python. For this I want to make use of the fact that a 2d power diagram can be interpreted as the intersection of a regular 3d voronoi diagr…

Where can I see the list of built-in wavelet functions that I can pass to scipy.signal.cwt?

scipy.signal.cwts documentation says:scipy.signal.cwt(data, wavelet, widths)wavelet : functionWavelet function, which should take 2 arguments. The first argument is the number of points that the return…

How to play sound in Python WITHOUT interrupting music/other sounds from playing

Im working on a timer in python which sounds a chime when the waiting time is over. I use the following code:from wave import open as wave_open from ossaudiodev import open as oss_opendef _play_chime()…

How can I use click to parse a string for arguments?

Say I have a list of strings containing arguments and options, with argparse, I’m able to parse this list using the parse_args function into an object, as follows:import argparseextra_params = [‘—su…

Tornado and WTForms

I am using WTForms for the first time. Using WTForms to validate POST requests in Tornado Below is my forms forms.pyclass UserForm(Form):user = TextField(user, [validators.Length(min=23, max=23)])In t…

Modifying a python docstring with a decorator: Is it a good idea?

A python docstring must be given as a literal string; but sometimes its useful to have similar docstrings for several functions (e.g., different constructors), or several access methods might accept th…