Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > fr.comp.lang.python > #3798
| From | Carboleum <c4rboleum**NOSPAM**@gmail.com.invalid> |
|---|---|
| Newsgroups | fr.comp.lang.python |
| Subject | Re: pandas switch-on switch-off |
| Date | 2022-04-03 14:51 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <t2c598$rs7$1@dont-email.me> (permalink) |
| References | <s46nio$r79$1@dont-email.me> <sc9kba$gpc$1@dont-email.me> |
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)
Back to fr.comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
pandas switch-on switch-off carboleum <c4rboleum-NOSPAM@gmail.com> - 2021-04-02 11:19 +0200
Re: pandas switch-on switch-off Damien Wyart <damien.wyart@free.fr> - 2021-04-02 11:34 +0200
Re: pandas switch-on switch-off carboleum <c4rboleum-NOSPAM@gmail.com> - 2021-04-02 12:25 +0200
Re: pandas switch-on switch-off Damien Wyart <damien.wyart@free.fr> - 2021-04-02 16:47 +0200
Re: pandas switch-on switch-off carboleum <c4rboleum-NOSPAM@gmail.com> - 2021-04-02 18:29 +0200
Re: pandas switch-on switch-off Nicolas <nicolasp@aaton.com> - 2021-04-06 08:53 +0200
Re: pandas switch-on switch-off Damien Wyart <damien.wyart@free.fr> - 2021-04-06 18:42 +0200
Re: pandas switch-on switch-off Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2021-04-06 22:00 +0200
Re: pandas switch-on switch-off Nicolas <nicolasp@aaton.com> - 2021-04-07 08:50 +0200
Re: pandas switch-on switch-off carboleum <c4rboleum-NOSPAM@gmail.com> - 2021-07-09 15:52 +0200
Re: pandas switch-on switch-off Damien Wyart <damien.wyart@free.fr> - 2021-07-15 11:23 +0200
Re: pandas switch-on switch-off Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2021-07-15 13:20 +0200
Re: pandas switch-on switch-off Carboléüm <c4rboleumNOSPAM@gmail.com> - 2021-07-16 10:13 +0200
Re: pandas switch-on switch-off Carboleum <c4rboleum**NOSPAM**@gmail.com.invalid> - 2022-04-03 14:51 +0200
Re: pandas switch-on switch-off Damien Wyart <damien.wyart@free.fr> - 2022-04-12 07:34 +0200
csiph-web