Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > fr.comp.lang.python > #3448

Re: pandas switch-on switch-off

From carboleum <c4rboleum-NOSPAM@gmail.com>
Newsgroups fr.comp.lang.python
Subject Re: pandas switch-on switch-off
Date 2021-04-02 18:29 +0200
Organization A noiseless patient Spider
Message-ID <s47gpj$9vj$1@dont-email.me> (permalink)
References <s46nio$r79$1@dont-email.me> <6066e50d$0$32516$426a74cc@news.free.fr> <s46rfk$kkb$1@dont-email.me> <60672e97$0$21601$426a74cc@news.free.fr>

Show all headers | View raw


On 2/04/21 16:47, Damien Wyart wrote:

> Je vois deux possibilités (donc l'une avec une boucle) :
> 
> ,----
> | #!/usr/bin/python3
> |
> | import pandas as pd
> |
> | 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'])
> | df2 = df.copy()
> |
> | df.at[0, 'res'] = False
> |
> | for i in range(1, len(df)):
> |     df.at[i, 'res'] = False if df.at[i, 'off'] else df.at[i, 'on'] or df.at[i-1, 'res']
> |
> | df['res'] = df['res'].astype(int)
> |
> | print(df)
> | print()
> |
> | ###
> |
> | def res(on, off):
> |     global value
> |     value = False if off else on or value
> |     return value
> |
> | value = False
> | df2.at[0, 'res'] = False
> |
> | df2.loc[1:, 'res'] = df2.loc[1:].apply(lambda row: res(*row[['on', 'off']]), axis=1)
> |
> | df2['res'] = df2['res'].astype(int)
> |
> | print(df2)
> | print()
> `----
> 

La solution 2 me plaît. C'est vers elle que je voulais aller mais sans y 
parvenir. Grand merci :-)

Back to fr.comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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