user4900645
user4900645

Reputation:

Comparing percent changed to previous row in pandas DataFrame

I am working with a particularly big dataframe which contains a lot of raw data. I think I have managed to convert the row I am interested in into percent changed with the pct_changed() and inserted it into a new column in the dataframe.

From there I want to compare each row, and get out a result only if the percent exceeds 2%. I tried with conditionals but it printed out a line for each row I had. Here is the code I am working with:

import pandas as pd

df=pd.read_csv("tempcsv.csv")
percentile = df['Pressure'].pct_change().fillna(0)

for row in percentile:
    if row > 2:
        print("Above")

What I am trying to do is to store the amount of returned "aboves", into a variable which I can use later on. I am pretty new to python so I am learning as I go.

I hope this is explained well enough!

PS: Would it also be possible to take out the median of each 10 rows?

Upvotes: 2

Views: 2516

Answers (1)

jezrael
jezrael

Reputation: 862406

You can create condition and sum Trues values, which are processing like 1s:

percentile = df['Pressure'].pct_change().fillna(0)

a = (percentile > 2).sum()

Upvotes: 1

Related Questions