dinsdag 3 maart 2020

Create pandas columns based on multiple conditions

Create columns based on other values in the row


 df1['Verschil']=df1.apply(lambda x : x['aantal_new'] - x['aantal_old'], axis=1)


Create pandas columns based on multiple conditions


With pandas and numpy we barely have to write our own functions, especially since our own functions will perform slow because these are not vectorized and pandas + numpy provide a rich pool of vectorized methods for us.

In this case your are looking for np.select since you want to create a column based on multiple conditions:

definieer 2 lijsten van dezelfde lengte
1 lijst met de where clauses
1 lijst met de values


lstValues=[(df1['aantal_new'] - df1['aantal_old']) /(df1['aantal_old']),'99999999999']

lstConditions = [df1['aantal_old'].notna() & df1['aantal_new'].notna(), df1['aantal_old'].isna() &  

df1['aantal_new'].isna()]
 

df1['Stijging2']= np.select(lstConditions,lstValues,'99')

Geen opmerkingen:

Een reactie posten

Datums bepalen adhv begin en einddatum in Dataframe

Voorbeeld op losse velden  ####################################################################### # import necessary packages from datetime...