Skip to content Skip to sidebar Skip to footer

Updating Information In Dataframe Column

I have a filtered dataset, new_df, like this Label New_Label Username Look_up 59 1.0 True vald21 val 67 1.0 True 2512 2512 75 1.0

Solution 1:

You can try merging two dataframe and then using .assign along with np.where. When merging with outer, the values not present will have NA so np.where with notnull() can be used:

pd.merge(df, new_df, how='outer').assign(Label = lambda row:np.where(row['New_Label'].notnull(), 0, 1))

If you do not want New_Label, you can drop the column with .drop('New_Label', axis=1). Something like below (if written in one line):

pd.merge(df, new_df, how='outer').assign( Label = lambda row:  np.where(row['New_Label'].notnull(), 0, 1)).drop('New_Label', axis=1)

Solution 2:

If I understand your question right, you want to flip 'New_Label', convert it to int and assign it to 'Label':

new_df['Label'] = (new_df['New_Label']==False).astype(int) 

Post a Comment for "Updating Information In Dataframe Column"