Python Forum
How to apply a class method to an entire dataframe column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to apply a class method to an entire dataframe column
#1
How can i apply a class method to an entire dataframe?

A minimal code is here for the problem:

I have a Price time series dataframe df with these columns column 1 date is a data object, other columns are float:

Date | Open | High | Low | Close
I have a method of a class as below:

class MyScanner():
def __init__(self, df, scandate):
self.df = df
self.mydate = scandate

#get the index to current date
self.currentindex = df[df['Date'] == scandate].index[0]

#Next Date High Low
self.d1 = self.df[self.df.index == self.currentindex+1]
self.h1 = self.d1['High'].max()
self.l1 = self.d1['Low'].min()

#Similarly assigned h2,l2,h3,l3

# stock price have made lower high and lower lows for next 3 days
def isbearish():
if (self.h1 > self.h2 > self.h3) &\
(self.l1 > self.l2 > self.l3):
return True

# stock have made lower high and lower lows for next 3 days
def isbullish():
if (self.h1 < self.h2 < self.h3) &\
(self.l1 < self.l2 < self.l3):
return True

# similar other functions which uses next 3 days high lows logic
def issomething():
..check something..
return True
So now I can run this for a particular date:

MyScanner(df, Date).isbullish()
This will return True if the next 3 'Date' matches the called method condition.

Now my query is I want to create a new Column called 'ISBullish', and apply this above method for respective dates in the Date Column to the whole data frame. This new Column will have either True or None, based on next 3 days data.

I am stuck with the syntax to be passed to the apply method:

df['ISBullish'] = df.Date.apply(MyClass(df, somedate).isbullish())
Reply
#2
Hi @tirtha9,

Good day!

Hope the below answer helps

df2['ISBearish'] = df2['Date'].apply(lambda x: (Scanner(df2, x).isbearish2()))
print(df2)
Output:
Date Open High Low Close ISBearish 0 2020-01-31 788.00 791.50 764.00 767.1 True 1 2020-02-01 771.85 798.00 728.40 742.8 None 2 2020-02-03 754.00 794.95 752.45 785.7 None 3 2020-02-04 784.70 794.50 773.60 776.9 None
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class definition and problem with a method HerrAyas 2 221 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 376 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Adding PD DataFrame column bsben 2 298 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 721 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Make entire script run again every 45 mo NDillard 0 316 Jan-23-2024, 09:40 PM
Last Post: NDillard
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 727 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Difference one column in a dataframe Scott 0 637 Feb-10-2023, 08:41 AM
Last Post: Scott
  splitting a Dataframe Column in two parts nafshar 2 948 Jan-30-2023, 01:19 PM
Last Post: nafshar
  Using one child class method in another child class garynewport 5 1,562 Jan-11-2023, 06:07 PM
Last Post: garynewport
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 827 Sep-08-2022, 06:32 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

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