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
  update values in one dataframe based on another dataframe - Pandas iliasb 2 362 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 474 Jun-22-2021, 07:57 PM
Last Post: snippsat
Question [Solved] How to refer to dataframe column name based on a list lorensa74 1 562 May-17-2021, 07:02 AM
Last Post: lorensa74
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 805 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  How to search for specific string in Pandas dataframe Coding_Jam 1 874 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  PANDAS: DataFrame | White Spaces & Special Character Removal traibr 1 1,486 Sep-10-2020, 07:02 PM
Last Post: eddywinch82
  No Output In Pandas DataFrame Query eddywinch82 1 805 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  strange error from pandas dataframe djf123 1 1,860 Jul-27-2020, 05:25 AM
Last Post: scidam
  [pandas] How to reshape the list Mekala 6 1,744 Jul-26-2020, 12:49 AM
Last Post: Mekala
  Pandas DataFrame not updating HelpMePlease 3 1,056 Jul-11-2020, 07:19 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