For loop using np.where
问题 I'm trying to create a new column in a dataframe that labels animals that are domesticated with a 1. I'm using a for loop, but for some reason, the loop only picks up the last item in the pets list. dog , cat , and gerbil should all be assigned a 1 under the domesticated column. Anyone have a fix for this or a better approach? df = pd.DataFrame( {'creature': ['dog', 'cat', 'gerbil', 'mouse', 'donkey'] }) pets = ['dog', 'cat', 'gerbil'] for pet in pets: df['domesticated'] = np.where(df[