Warning looping Python Pandas, How to make the looping differently?

Asked

Viewed 198 times

1

Guys I’m doing this looping here:

for i in range(1, len(candles)):
    if candles['askclose'][i]> candles['askopen'][i]:
        candles['Fechamento'][i]= 'alta'

But the jupyternotebook always returns me this Warning and sometimes hangs and does not advance, some idea of how to improve or make different this looping, to avoid Warning:

Warning:

C:\Users\Jair\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  This is separate from the ipykernel package so we can avoid doing imports until

The mask as the friend suggested is great solution, but when I have a bigger loop like this:

for i in range(1, len(candles)):
    tamanho_twin = 0
    if candles['Fechamento'][i]=='baixa' and candles['Fechamento'][i-1] == 'alta':
        if candles['askclose'][i] <= candles['askopen'][i-1] and candles['MA20'][i]<candles['MA20'][i-2]:
            limite_sombraV= ((candles['askclose'][i]-candles['askopen'][i])*0.1)+candles['askclose'][i]
            if candles['asklow'][i] == limite_sombraV:
                tamanho_twin = candles['askclose'][i]+candles['askopen'][i]
                candles['Twintower'][i] = 0
                candles['Tamanho_Twin'][i] = candles['askclose'][i]+candles['askopen'][i]
  • I couldn’t just do: candles['Fechamento'] = np.where(candles.askclose > candles.askopen, 'alta', ''), nor will you need the bow for.

  • Can you show "Candles"? Make a print (print(candles)) and show the result, or part of it.

  • Sorry, now it’s another question.

  • @Noobsaibot, not why I also need to include the invesrso for 'low'

  • @Jairmiranda just put the third parameter: candles['Fechamento'] = np.where(candles.askclose > candles.askopen, 'alta', 'baixa'), the first is the condition, second and third are the values that will be applied if the condition is true or false.

  • @Noobsaibot has a problem only I have the third situation, where Candles.askclose=Candles.askopen, there in this case do not know how to apply np.Where

  • @Noobsaibot, these are 3 situations: Candles.askclose > Candles.askopen: high, Candles.askclose < Candles.askopen: 'low, Candles.askclose > Candles.askopen: 'neutral'

  • 1

    @Jairmiranda does so: candles['Fechamento'] = np.where(&#xA; candles.askclose < candles.askopen,&#xA; 'baixa',&#xA; np.where(candles.askclose == candles.askopen, 'neutro', 'alta'))

  • @Noobsaibot, your solution was fantastic, I used for absolutely all loopings, thank you very much

Show 5 more comments

1 answer

0

If I understand the question you only need a mask, see:

import pandas as pd

a = [[1,2,'n/a'],[2,1,'n/a'],[3,4,'n/a'],[4,3,'n/a']]
candles = pd.DataFrame(a,columns=['askclose', 'askopen', 'fechamento'])

print('Candles original', candles, sep='\n')
Candle original
   askclose  askopen fechamento
0         1        2        n/a
1         2        1        n/a
2         3        4        n/a
3         4        3        n/a

# Criação da mascara
mask = candles.askopen>candles.askclose

# Atualização do candles
candles.loc[mask, 'fechamento'] = 'alta'

print('Candles atualizado', candles, sep='\n')
Candles atualizado
   askclose  askopen fechamento
0         1        2       alta
1         2        1        n/a
2         3        4       alta
3         4        3        n/a

Obs.:
To assign a different value when askopen is smaller, create a second mask or deny the mask created with a bitwise operator (candles.loc[~mask, 'fechamento'] = 'baixa')

See working on repl.it.

Edited
After editing the question I understood that your need is to iterate the dataframe, I created a function that iterates the df received and changes it based on the previous value of a column, to simplify I used numeric values and column names based on the first 3 letters (a, b, c). Just adapt to your needs.

import pandas as pd
import numpy as np

def iter_df(df):
    for i in range(0, len(df)):
        if i !=0: 
            print('indice atual: ',i, 'coluna: a')
            print('valor atual :', df.iloc[i]['a'], 'valor anterior: ', 
                   df.iloc[i-1]['a'])
            print('-'*36)

        # Alterando o valor de uma coluna baseado no valor do indice anterior
        if df.iloc[i-1]['a']==11:
            df.iloc[i]['a']=99
    return df 

a = np.array([[1, 2, 3], [11, 12, 13], [21, 22, 23], [31, 32, 33], [41, 42, 43]])
df = pd.DataFrame(a, columns=['a', 'b', 'c'])

print ('df antes da atualizacao', df, sep='\n')
df antes da atualizacao
    a   b   c
0   1   2   3
1  11  12  13
2  21  22  23
3  31  32  33
4  41  42  43

print ('df pós atualizacao', iter_df(df),sep='\n' )
indice atual:  1 coluna: a
valor atual : 11 valor anterior:  1
------------------------------------
indice atual:  2 coluna: a
valor atual : 21 valor anterior:  11
------------------------------------
indice atual:  3 coluna: a
valor atual : 31 valor anterior:  21
------------------------------------
indice atual:  4 coluna: a
valor atual : 41 valor anterior:  31
------------------------------------
df pós atualizacao
    a   b   c
0   1   2   3
1  11  12  13
2  99  22  23
3  31  32  33
4  41  42  43

Look at this new version in repl.it.

  • Thank you very much, but take a look at what I added above, when you are going to have a bigger looping

  • Okay, but you totally changed the question and put a code that doesn’t work, you can simulate a dataframe to run with the code?

  • Even without clearly understanding what you want, I drafted a new version of the answer, I’ll edit this, see if it fits now.

  • Thank you, so I have several iterations but with different conditions, the idea is to find patterns within a dataframe, so I’ll have to find a solution where I can adapt to other conditions, I really liked your first solution well pythonica, I’ll try to adapatar her to a higher condition and put here for you!

  • To find patterns in data these days is very easy, use a simple neural network (with few layers). In python we have an excellent framework: Pytorch.

  • would it replace these conditions I want to create? Like I set a pattern and I want every time he finds the pattern, he does x activity, but it’s not graphically, it’s reading data.

  • Neural networks identify patterns only (which is no small thing), the algorithm to process the patterns you would have to do, but I think my second solution does exactly what you ask after you edit the question.

  • 1

    I read here about neural networks a while ago, already esotu doing the course, I started as soon as I read your observation, I think it will be perfect and will allow a more comprehensive system evolution complex and correct, because the neural network does exactly what I want, since I want that after 3 or 5 conditions he gives me a pattern, nothing better than to filter by the 'neuronios', I will only dedicate myself Saturday and Sunday to finish the course of the pytorch and apply here

  • 1

    If your answer has been helpful, please accept and upvote. :-)

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.