Python Forum

Full Version: Comparing results within a list and appending to pandas dataframe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am doing a dummy project to hone my python skills and there is a problem I am encountering. I have a pandas column with many values inside it, I want to do the following (I have set chunksize = 1440 because I want to process the data in groups of 1440's and store the output for each group of 1440 separately.):

Take the first value of the column (let it be 'x')
Go through the remaining (1439) values, if you find a value greater than 'x' let it be 'y'.
If you find an entry whose value is 10 more than 'y' enter 'Profit' into a separate column. Or
If you find an entry whose value is 10 less than 'y' enter 'Loss' into a separate column.
This is what I have tried:

import pandas as pd

for df in pd.read_csv('C:/Users/Workstation/.spyder-py3/AAPL.USUSD_Candlestick_1_M_BID_14.11.2018-28.11.2020 (1).csv',chunksize=1440):

df = df[ df["Volume"] != 0]

df['Changing'] = ''

df.loc[(df.High.shift() - df.High).where(lambda x:x>3).dropna().index+1, 'Changing'] = "Profit"

df.loc[(df.High.shift() - df.High).where(lambda x:x<3).dropna().index+1, 'Changing'] = "Loss"
But I get the error:
'[960] not in index'
I think the loop does not stop when the column ends. Is there a way to fix this?

I know it's a lot, but any help will be greatly appreciated.

Thanks in advance...
Can you post a runnable code with a subset of your data, and the complete traceback?