python - Fill NaN values -
i have dataframe
timestamp p_act_kw periode_tarif p_souscr 2016-01-01 00:00:00 116 hc 250 2016-01-01 00:10:00 121 hc 250 2016-01-01 00:20:00 121 nan 250
to use dataframe, must fill nan values (hc or hp) based on condition:
if (hour extracted timestamp in {0,1,2, 3, 4, 5, 22, 23}
so replace nan hc, else hp. did function:
def prep_data(data): data['periode_tarif']=np.where(data['periode_tarif']in (0, 1,2, 3, 4, 5, 22, 23),'hc','hp') return data
but error:
valueerror traceback (most recent call last) <ipython-input-23-c1fb7e3d7b82> in <module>() ----> 1 prep_data(df_energy2) <ipython-input-22-04bd325f91cd> in prep_data(data) 1 # nettoyage des données 2 def prep_data(data): ----> 3 data['periode_tarif']=np.where(data['periode_tarif']in (0, 1),'hc','hp') 4 return data c:\users\demonstrator\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self) 890 raise valueerror("the truth value of {0} ambiguous. " 891 "use a.empty, a.bool(), a.item(), a.any() or a.all()." --> 892 .format(self.__class__.__name__)) 893 894 __bool__ = __nonzero__ valueerror: truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().
how can fix this?
use isin
test membership:
data['periode_tarif']=np.where(data['periode_tarif'].isin([0, 1,2, 3, 4, 5, 22, 23]),'hc','hp')
in
doesn't understand how evaluate array of boolean values becomes ambiguous if have more 1 true
in array hence error
Comments
Post a Comment