in Education by
I have a function that returns False and True based on conditions defined in the function below: def find_trend(data, period:int): ''' Inputs: takes in a dataframe and an interger Outputs: returns True if the trend of the simple moving average over given period is positive, else returns False ''' data['SMA'] = data['close'].rolling(period).mean() return (data['SMA']-data['SMA'].shift(1)) > 0 I would like to see the exact amount of values returning True and False. Is there a way to output this via print()? aapl['trend'] = find_trend(aapl, 4) aapl.trend.head() 0 False 1 False 2 False 3 False 4 False 5 False 6 False 7 True 8 True 9 True 10 False 11 False 12 False 13 False 14 True 15 True 16 True 17 True 18 False 19 False 20 False 21 True 22 False 23 False 24 False 25 True 26 False 27 False 28 False 29 True Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
(data['SMA']-data['SMA'].shift(1)) > 0 only creates a bool. create a variable for the value and for the bool and return both. data['SMA'] = data['close'].rolling(period).mean() will update the dataframe inplace, meaning a SMA column will get added to aapl. def find_trend(data, period: int): ''' Inputs: dataframe and an interger Outputs: returns True if the trend of the simple moving average over given period is positive, else returns False ''' sma = data['Close'].rolling(period).mean() # creates a series with the rolling mean diff = sma - sma.shift(1) # calculates a series of values greater_than_0 = diff > 0 # creates a series of bools return diff, greater_than_0 aapl['value'], aapl['trend'] = find_trend(aapl, 4) # display(aapl) Date High Low Open Close Volume Adj Close ticker value trend 0 2019-01-31 169.000000 164.559998 166.110001 166.440002 40739600.0 162.852737 aapl NaN False 1 2019-02-01 168.979996 165.929993 166.960007 166.520004 32668100.0 162.931030 aapl NaN False 2 2019-02-04 171.660004 167.279999 167.410004 171.250000 31495500.0 167.559082 aapl NaN False 3 2019-02-05 175.080002 172.350006 172.860001 174.179993 36101600.0 170.425934 aapl NaN False 4 2019-02-06 175.570007 172.850006 174.649994 174.240005 28239600.0 170.484650 aapl 1.950001 True 5 2019-02-07 173.940002 170.339996 172.399994 170.940002 31741700.0 167.255768 aapl 1.105000 True 6 2019-02-08 170.660004 168.419998 168.990005 170.410004 23820000.0 167.452316 aapl -0.209999 False 7 2019-02-11 171.210007 169.250000 171.050003 169.429993 20993400.0 166.489288 aapl -1.187500 False 8 2019-02-12 171.000000 169.699997 170.100006 170.889999 22283500.0 167.923965 aapl -0.837502 False If you are a beginner and want to know more about Data Science the do check out the Data Science course

Related questions

0 votes
    I have the data frame that looks like this: Date Visa Mastercard Amex Paypal 1/1/20 0 20 0 0 2/1/20 ... info() Any help is appreciated Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I want to calculate a percentage, for each id, of True values from all rows of the id. Here an example ... df.num_true/df.num_col1_id Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have data like this: id a b c d 1 y y z z 2 y z y y 3 y y y y I want to count the value of "y" ... 1 2 2 1 3 4 Can anyone help me? Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have a big data frame. I want to replace float 1.0 and 0.0 with the true and false. My code: import pandas ... to do it in one line. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I want to save files for each result from the loop. For example, I wrote the code but it only saves one file ... do I modify my code? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am trying to write my below pinging script results into the Text file, but I am getting an error message. ... object is not iterable Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Subtotal value is the data base are calculated with count function are true ya false Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    Subtotal value is the data base are calculated with count function are true ya false Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    I have the function and want to create a new column df['growth_factor'] which will have a derived value in it. ... can I achieve this? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I am trying to create a new column where in, True if last n rows are True in other column. It works fine ... better way to achieve it? Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    Is it possible that a function have more than one return statements? ,1.true,2.false Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    If I want to use the BatchNormalization function in Keras, then do I need to call it once only at the ... much of a difference. Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    I have designed the simple function that looks at an inputted list of numbers, identifies a minimum and maximum ... into the list? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    SA=1000.00 AI=0.12 MP=100.00 def remainb(x): if x==0: return 0 else: return x=(SA+(AI/12)*SA)-MP for ... . run the loop until SA==0). Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Suppose I have a Tensorflow tensor. How do I get the dimensions (shape) of the tensor as integer values? I ... 'Dimension' instead. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
...