Path: ...!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Carboleum Newsgroups: fr.comp.lang.python Subject: Re: pandas switch-on switch-off Date: Sun, 3 Apr 2022 14:51:52 +0200 Organization: A noiseless patient Spider Lines: 105 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 3 Apr 2022 12:51:52 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="9161ca3124d6aed1ada46b2eff535ae5"; logging-data="28551"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5p50LmQDEI/oxdMlrtMYf" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 Cancel-Lock: sha1:qXgTPMcclEggP023aeXdFvkqYC8= In-Reply-To: Content-Language: fr-BE Bytes: 3978 On 9/07/21 15:52, carboleum wrote: > On 2/04/21 11:19, carboleum wrote: >> >> Bonjour, >> >> Dans mon dataframe, j'ai une colonne "on" qui allume la lumière et une >> colonne "off" qui éteint la lumière. >> >> Quelqu'un connais la fonction (si elle existe ?) qui renvoie la >> colonne "lumière allumée" ? >> >>      on  off  res >> 0    0    0    0 >> 1    0    0    0 >> 2    1    0    1 >> 3    0    0    1 >> 4    0    1    0 >> 5    0    0    0 >> 6    0    0    0 >> 7    0    1    0 >> 8    0    0    0 >> 9    1    0    1 >> 10   0    0    1 >> 11   1    0    1 >> 12   0    0    1 >> 13   0    1    0 >> 14   0    1    0 >> 15   0    0    0 >> >> Ou une piste ? >> >> Merci > > J'ai trouvé \o/ Yeah!... > > C'est un peu tiré par les cheveux, mais ca me plait! :-) > > df = pd.DataFrame([[0, 0], [0, 0], [1, 0], [0, 0], [0, 1], [0, 0], [0, > 0], [0, 1], [0, 0], [1, 0], [0, 0], [1, 0], [0, 0], [0, 1], [0, 1], [0, > 0], [0, 0], [1, 0], [1, 1], [0, 0]], columns=['on', 'off']).astype(bool) > > df['sig'] = df.on.where(df.on).fillna(1 - df.off.where(df.off)).ffill() > > df > >     on  off  sig > 0    0    0  NaN > 1    0    0  NaN > 2    1    0  1.0 > 3    0    0  1.0 > 4    0    1  0.0 > 5    0    0  0.0 > 6    0    0  0.0 > 7    0    1  0.0 > 8    0    0  0.0 > 9    1    0  1.0 > 10   0    0  1.0 > 11   1    0  1.0 > 12   0    0  1.0 > 13   0    1  0.0 > 14   0    1  0.0 > 15   0    0  0.0 > 16   0    0  0.0 > 17   1    0  1.0 > 18   1    1  1.0 > 19   0    0  1.0 > > Bonjour Bonjour, J'ai pondu quelque chose qui est plus lisible: df['sig0'] = df.on - df.off df['sig1'] = df.sig0.where(df.sig0 != 0).ffill() df['sign'] = df.sig1 > 0 df on off sig0 sig1 sign 0 0 0 0 NaN False 1 0 0 0 NaN False 2 1 0 1 1.0 True 3 0 0 0 1.0 True 4 0 1 -1 -1.0 False 5 0 0 0 -1.0 False 6 0 0 0 -1.0 False 7 0 1 -1 -1.0 False 8 0 0 0 -1.0 False 9 1 0 1 1.0 True 10 0 0 0 1.0 True 11 1 0 1 1.0 True 12 0 0 0 1.0 True 13 0 1 -1 -1.0 False 14 0 1 -1 -1.0 False 15 1 1 0 -1.0 False 16 0 0 0 -1.0 False 17 0 0 0 -1.0 False 18 1 0 1 1.0 True 19 1 1 0 1.0 True 20 0 0 0 1.0 True et qui a pour effet d'annuler l'effet quand on appuie sur les deux boutons en même temps (ligne 15 et 19)