Thomas LESIEUR
Thomas LESIEUR

Reputation: 427

How to apply np.where() to a search into a dataframe?

I got to dataframe :

I would like to use as a condition the line in comment :

np.where(
    #   df_depart['NOM SCADA'] in data_sorted_monotone.columns,
    df_depart['NOM SCADA'] =='test',
    'smth',
    np.nan
)

I would like to create a new row in df_depart, this row will be filled by a value from a calcul in data_sorted_monotone. For this i need to know when a value of the column 'Varaible_scada' is included as a column name in data_sorted_monotone. If not i won't do the calculation and fill 'Nan'.

Does somebody have a clean and fast way to do it ?

Thanks !!

Upvotes: 0

Views: 2238

Answers (1)

Da  Song
Da Song

Reputation: 558

let me know if I get what you are talking about, but try this:

df_depart['new_col'] = np.where(df_depart['NOM SCADA'].isin(data_sorted_monotone.columns), 'smth', np.nan)

Upvotes: 2

Related Questions