Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 892
Roberto Avatar asked Jul 19 '17 08:07

Roberto


People also ask

How do you check if a column value is in a list Pandas?

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.

How do you check if something is in a Pandas series?

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.


1 Answers

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
like image 68
jezrael Avatar answered Oct 12 '22 10:10

jezrael