Python pandas - new column's value if the item is in the list
12,306
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
Author by
Roberto
Updated on June 07, 2022Comments
-
Roberto almost 2 years
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 should indicate if country from dataframe is in the list or not.
Below is the shortened version of the code:
import pandas as pd import numpy as np EU = ["Austria","Belgium","Germany"] df1 = pd.DataFrame(data={"Country":["USA","Germany","Russia","Poland"], "Capital":["Washington","Berlin","Moscow","Warsaw"]}) df1["EU"] = np.where(df1["Country"] in EU, "EU", "Other")
The error I get is:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I don't know what the problem is and how to solve it. What am I missing?