Python Forum
Comparing results within a list and appending to pandas dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing results within a list and appending to pandas dataframe
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?

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 262 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  How to search for specific string in Pandas dataframe Coding_Jam 1 326 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  PANDAS: DataFrame | White Spaces & Special Character Removal traibr 1 615 Sep-10-2020, 07:02 PM
Last Post: eddywinch82
  No Output In Pandas DataFrame Query eddywinch82 1 457 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  strange error from pandas dataframe djf123 1 1,046 Jul-27-2020, 05:25 AM
Last Post: scidam
  [pandas] How to reshape the list Mekala 6 784 Jul-26-2020, 12:49 AM
Last Post: Mekala
  Pandas DataFrame not updating HelpMePlease 3 669 Jul-11-2020, 07:19 PM
Last Post: jefsummers
  Pandas DataFrame visual Truman 8 899 Jul-10-2020, 06:11 AM
Last Post: hussainmujtaba
  Pandas DataFrame and unmatched column sritsv19 0 707 Jul-07-2020, 12:52 PM
Last Post: sritsv19
  Pandas DataFrame Concatenate problems Kristenl2784 1 537 Jul-01-2020, 01:28 AM
Last Post: hussainmujtaba

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020