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
#1
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...
Reply
#2
Can you post a runnable code with a subset of your data, and the complete traceback?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to most effectively unpack list of name-value pair dictionaries in a dataframe? zlim 1 610 Nov-07-2023, 10:56 PM
Last Post: zlim
  HTML Decoder pandas dataframe column mbrown009 3 961 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,089 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,396 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,266 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,241 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,757 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,525 Jan-10-2022, 04:42 PM
Last Post: moduki1
  Describe repeating sequences in Dataframe and plot results PBRM 0 1,249 Dec-06-2021, 10:29 PM
Last Post: PBRM
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,099 Aug-14-2021, 12:38 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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