Python pandas - new column's value if the item is in the list

前端 未结 1 1595
迷失自我
迷失自我 2020-12-29 13:48

I want to create a new column in pandas dataframe. The first column contains names of countries. The list contains countries I am interested in (eg. in EU). The new colum sh

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 14:52

    Use isin for check membership:

    df1["EU"] = np.where(df1["Country"].isin(EU), "EU", "Other")
    print (df1)
          Capital  Country     EU
    0  Washington      USA  Other
    1      Berlin  Germany     EU
    2      Moscow   Russia  Other
    3      Warsaw   Poland     EU
    

    0 讨论(0)
提交回复
热议问题