【发布时间】:2020-10-23 21:20:05
【问题描述】:
df = {'Fruit':['Apple','Banana','Cherries','Dragonfruit','Elderberry']}
pd.DataFrame(df)
我想根据水果变量创建一个新列。
预期输出:
Fruit Quantity
Apple 10
Banana 5
Cherries 3
Dragonfruit 2
Elderberry 1
代码:
df['Quantity']= np.where(df['Fruit']=='Apple',10, df['Fruit']=='Banana',5,
df['Fruit']=='Cherries',3,df['Fruit']=='Dragonfruit',2,1)
```
shows the error:
TypeError: where() takes from 1 to 3 positional arguments but 9 were given
【问题讨论】:
-
np.select((cond1, cond2), (select1, select2), default)
标签: python pandas numpy dataframe