Python: get the max value with the location above and below than the max

2024/11/18 20:18:11

If I have a dataframe like this,

index  User    Value  location1      1      1.0    4.5 2      1      1.5    5.23      1      3.0    7.04      1      2.5    7.55      2      1.0    11.56      2      1.25   14.17      2      2.0    13.08      2      3.5    14.09      3      2.0    2.410     3      3.5    4.311     3      1.0    5.412     3      3.0    5.313     4      1.0    11.014     4      3.0    12.3

How would I select the max VALUE for each user with the nearest neighbor (above and lower) location of the max location, and if there's only one nearest location of the max, retrieve them as well?

The final results should be like this,

index  User   Value  location 2      1      1.5    5.23      1      3.0    7.04      1      2.5    7.56      2      1.25   14.17      2      2.0    13.08      2      3.5    14.09      3      2.0    2.410     3      3.5    4.312     3      3.0    5.313     4      1.0    11.014     4      3.0    12.3
Answer
import pandas as pd# df = "your_data_frame"new_df = pd.DataFrame({"ID": [],"User": [],"Value": []})for user in sorted(list(set(df['User'].to_list()))):new_df = pd.concat([new_df, df[df['User'] == user].nlargest(3, 'location')])
https://en.xdnf.cn/q/120028.html

Related Q&A

Retrieve smart cards PAN with Python and pyscard

Im trying to retrieve the PAN of a smart card using pyscard in Python. What I have done so far is to connect to the reader and to retrieve various information about the reader and the card... but I can…

How to stop a specific key from working in Python

My laptop keyboard has a bug and it sometimes presses the number 5 randomly so i tried many things and they didnt work, I tried programming a code that can stop it but i couldnt because i am a beginner…

How do i sort a 2D array or multiple arrays by the length of the array using bubble sort

trying to write a Python function: def compare_lengths(x, y, z) which takes as arguments three arrays and checks their lengths and returns them as a triple in order of length. For example, if the funct…

How to split a string in Python by 2 or 3, etc [duplicate]

This question already has answers here:Split string every nth character(21 answers)How to iterate over a list in chunks(40 answers)Closed 10 years ago.Does anyone know if its possible in python to spli…

.LAS into a .CSV file using python

How to change a .las file into a .csv file? Have been trying myself but no luck no far. I am just looking for something decently short that will save some time when I have to convert big .olas files i…

using pandas read_excel to read from stdin

Note: I have solve this problem as per below:I can use to_csv to write to stdout in python / pandas. Something like this works fine:final_df.to_csv(sys.stdout, index=False)I would like to read in an a…

How to print a string x times based on user input [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Understanding for loops in Python(4 answers)Closed 1 year ago.I am trying to make a simple application that will pri…

How does .join work in Python?

I want to display each row of a SQL query result on a webpage. I found some code, but I dont understand what this line does.u"<br>".join([u"{0}".format(row.combination) for r…

Login, Navigate and Retrieve data behind a proxy with Python

I want, with a python script, to be able to login a website and retrieve some data. This behind my companys proxy.I know that this question seems a duplicate of others that you can find searching, but …

overflow in exp, python

cant really figure out why this error RuntimeWarning: overflow encountered in exp is showing up. The function Im trying to implement is:Id = lambda t_u, yp: Is * (np.exp((Vin(t_u) - L*yp)/(n_Ut*Ut)) - …