0

I posted a question similar before, but perhaps I was not clear with the info that I provided. I would like to introduce the values (Supervazio, Vazio...) in 1 column (Tariff) of an existing dataframe based on 2 weekly schedules (Summer and Winter).

enter image description here

My existing dataframe is:

    Potência Ativa  ...  
2019-01-01 00:00:00            31.0  ...               
2019-01-01 01:00:00            26.0  ...                    
2019-01-01 02:00:00            21.0  ...                      
2019-01-01 03:00:00            21.0  ...                      
2019-01-01 04:00:00            21.0  ... 

and I would like to turn into:

                   Potência Ativa  ...       Tarif
2019-01-01 00:00:00            31.0  ...     supervazio          
2019-01-01 01:00:00            26.0  ...      supervazio              
2019-01-01 02:00:00            21.0  ...      vazio              
2019-01-01 03:00:00            21.0  ...      vazio                
2019-01-01 04:00:00            21.0  ...      vazio

etc....

So I tried the previous recommendation,

consumption_year['Tarif'] = np.select([((consumption_year.index.hour >= 2) & (consumption_year.index.hour < 20)) & ((consumption_year.index.weekday >= 0) & (consumption_year.index.weekday <= 4))], [supervazio])

but there are so many conditions as you can see. One of my problems is that I can only call one time

consumption_year['Tarif'] = np.select(...

If not, I would do something like this:

#summer vazio - working day
consumption_year['Tarif'] = np.select(...

(is there a an alternative to add to existing consumption_year['Tarif'] instead of creating a new one?)

#summer vazio - sunday
consumption_year['Tarif'] = np.select(...

Or other simpler method?

PMendes
  • 29
  • 4
  • 1
    You can call multiple conditions with `np.select` see here :https://stackoverflow.com/a/19913845/9375102 – Umar.H Jun 23 '20 at 17:09
  • 1
    I guess `np.select` is still the easiest solution. In order to improve readability you can put all the logic to create the condition and choice lists into a separate function that returns a tuple of theses lists and maybe the default value and then use this function in `np.select` like `np.select(*tarifs())` – Stef Jun 24 '20 at 07:25

0 Answers0