Raagib khan
Raagib khan

Reputation: 47

how to compare the mean of previous three values of a column(dataframe) with the 4th value and so on..,if 4th value > mean,assign 1 else 0

How to execute the code, both with function and with lambda.mean should be calculated for previous 3 values and so on and compared with the consecutive 4th value.


def func(df,timestep=1):
    for i in range(df.columns):
        x = df['Close'].rolling(3).mean()
        y = df['Close'][3]+ timestep
        if y > x:
            return 1
        else:
            return 0


df['Sentiment'] = df['Close'].apply(func)

This gives an error: AttributeError: 'float' object has no attribute 'columns'

I dont even know after resolving this,this code would work for our result too.

Upvotes: 1

Views: 61

Answers (1)

mozway
mozway

Reputation: 260890

Use a rolling.mean + shift:

# set up example
np.random.seed(0)
df = pd.DataFrame({'Close': np.random.randint(0, 100, size=10)})

# apply rolling mean and shift
df['Sentiment'] = df['Close'].gt(df['Close'].rolling(3, min_periods=1)
                                            .mean().shift()
                                 ).astype(int)

Example:

   Close  Sentiment
0     44          0
1     47          1
2     64          1
3     67          1
4     67          1
5      9          0
6     83          1
7     21          0
8     36          0
9     87          1

Intermediate:

   Close  Sentiment  mean_3_prev
0     44          0          NaN
1     47          1    44.000000
2     64          1    45.500000
3     67          1    51.666667
4     67          1    59.333333
5      9          0    66.000000
6     83          1    47.666667
7     21          0    53.000000
8     36          0    37.666667
9     87          1    46.666667

Keeping NaNs:

# apply rolling mean and shift
s = df['Close'].rolling(3).mean().shift()

df['Sentiment'] = df['Close'].gt(s).astype(int).mask(s.isna())

Output:

   Close  Sentiment
0     44        NaN
1     47        NaN
2     64        NaN
3     67        1.0
4     67        1.0
5      9        0.0
6     83        1.0
7     21        0.0
8     36        0.0
9     87        1.0

Upvotes: 1

Related Questions