hem
被定义为接受两个参数,但在 apply
中你只传递一个。当你这样做时,你会将完整的continent
列传递给它。可能不是你想要的。
您可以使用嵌套的numpy
where
进行简化。
import numpy as np
df['hemisphere'] = np.where(df['continent'].isin(northern), 'northern', np.where(df['continent'].isin(southern),'southern','Not Found'))
结果
country code continent plants invertebrates vertebrates total hemisphere
0 Afghanistan AFG Asia 5 2 33 40 northern
1 Albania ALB Europe 5 71 61 137 northern
2 Algeria DZA Africa 24 40 81 145 southern
【讨论】: