Is there a way to find the largest change in a pandas dataframe column?

2024/7/7 7:17:33

Im trying to find the largest difference between i and j in a series where i cannot be before j. Is there an efficient way to do this in pandas:

x = [1, 2, 5, 4, 2, 4, 2, 1, 7]
largest_change = 0for i in range(len(x)):for j in range(i+1, len(x)):change = x[i] - x[j]print(x[i], x[j], change)if change > largest_change:largest_change = change

The output would just be the value, in this case 4 from 5 to 1.

Answer

Try numpy broadcast with np.triu and max

arr = np.array(x)
np.triu(arr[:,None] - arr)array([[ 0, -1, -4, -3, -1, -3, -1,  0, -6],[ 0,  0, -3, -2,  0, -2,  0,  1, -5],[ 0,  0,  0,  1,  3,  1,  3,  4, -2],[ 0,  0,  0,  0,  2,  0,  2,  3, -3],[ 0,  0,  0,  0,  0, -2,  0,  1, -5],[ 0,  0,  0,  0,  0,  0,  2,  3, -3],[ 0,  0,  0,  0,  0,  0,  0,  1, -5],[ 0,  0,  0,  0,  0,  0,  0,  0, -6],[ 0,  0,  0,  0,  0,  0,  0,  0,  0]])np.triu(arr[:,None] - arr).max()Out[758]: 4
https://en.xdnf.cn/q/119755.html

Related Q&A

Updating scikit-learn to latest version with Anaconda environment fails with http error 000

I use Anaconda3 installed on my pc Win10 64bits. I noticed it runs with an outdated scikit learn version (0.21.3), and I am trying to update it (0.24.1 available on https://repo.anaconda.com/pkgs/main/…

Python RegEx remove new lines (that shouldnt be there)

I got some text extracted and wish to clean it up by RegEx.I have learned basic RegEx but not sure how to build this one:str = this is a line that has been cut. This is a line that should start on a …

Python CSV writer

I have a csv that looks like this:HA-MASTER,CategoryID 38231-S04-A00,14 39790-S10-A03,14 38231-S04-A00,15 39790-S10-A03,15 38231-S04-A00,16 39790-S10-A03,16 38231-S04-A00,17 39790-S10-A03,17 38231-S04-…

How to perform standardization on the data in GridSearchCV?

How to perform standardizing on the data in GridSearchCV?Here is the code. I have no idea on how to do it.import dataset import warnings warnings.filterwarnings("ignore")import pandas as pd …

how to find the permutations of string? python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

Unicode category for commas and quotation marks

I have this helper function that gets rid of control characters in XML text:def remove_control_characters(s): #Remove control characters in XML textt = ""for ch in s:if unicodedata.category(c…

Uppercase every other word in a string using split/join

I have a string: string = "Hello World" That needs changing to: "hello WORLD" Using only split and join in Python. Any help? string = "Hello World" split_str = string.spl…

BeautifulSoup get text from tag searching by Title

Im scrapping a webpage with python that provides different documents and I want to retrieve some information from them. The document gives the information in two ways, theres this one where it gives it…

Subtract from first value in numpy array [duplicate]

This question already has answers here:Numpy modify array in place?(4 answers)Closed 6 years ago.Having numpy array like that:a = np.array([35,2,160,56,120,80,1,1,0,0,1])I want to subtract custom valu…

how to give range of a worksheet as variable

I am having one excel sheet which is used to read the data through python openpyxl...so in my script i have values that are hard coded as ws[E2:AB3] as AB3 is the last entry to be read...but now the sh…