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?
You can check if a column contains/exists a particular value (string/int), list of multiple values in pandas DataFrame by using pd. series() , in operator, pandas. series. isin() , str.
isin() function check whether values are contained in Series. It returns a boolean Series showing whether each element in the Series matches an element in the passed sequence of values exactly.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With