Ben.hardy
Ben.hardy

Reputation: 125

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

for i in range(0,len(donor_df)):
    if donor_df['cand_nm'][i] == 'Obama, Barack':
        donor_df['Party'] = 'Democrat'
    else:
        donor_df['Party'] = 'Republican'

donor_df is a dataframe want to create a new column named 'party' but its not giving an output

Upvotes: 0

Views: 55

Answers (1)

BENY
BENY

Reputation: 323276

You are looking for np.where

donor_df['Party']=np.where(donor_df['cand_nm'] == 'Obama, Barack','Democrat','Republican')

Upvotes: 1

Related Questions