in of a Series checks whether the value is in the index:
In [11]: s = pd.Series(list('abc'))
In [12]: s
Out[12]:
0 a
1 b
2 c
dtype: object
In [13]: 1 in s
Out[13]: True
In [14]: 'a' in s
Out[14]: False
One option is to see if it's in unique values:
In [21]: s.unique()
Out[21]: array(['a', 'b', 'c'], dtype=object)
In [22]: 'a' in s.unique()
Out[22]: True
or a python set:
In [23]: set(s)
Out[23]: {'a', 'b', 'c'}
In [24]: 'a' in set(s)
Out[24]: True
As pointed out by @DSM, it may be more efficient (especially if you're just doing this for one value) to just use in directly on the values:
In [31]: s.values
Out[31]: array(['a', 'b', 'c'], dtype=object)
In [32]: 'a' in s.values
Out[32]: True
Answer from Andy Hayden on Stack Overflowin of a Series checks whether the value is in the index:
In [11]: s = pd.Series(list('abc'))
In [12]: s
Out[12]:
0 a
1 b
2 c
dtype: object
In [13]: 1 in s
Out[13]: True
In [14]: 'a' in s
Out[14]: False
One option is to see if it's in unique values:
In [21]: s.unique()
Out[21]: array(['a', 'b', 'c'], dtype=object)
In [22]: 'a' in s.unique()
Out[22]: True
or a python set:
In [23]: set(s)
Out[23]: {'a', 'b', 'c'}
In [24]: 'a' in set(s)
Out[24]: True
As pointed out by @DSM, it may be more efficient (especially if you're just doing this for one value) to just use in directly on the values:
In [31]: s.values
Out[31]: array(['a', 'b', 'c'], dtype=object)
In [32]: 'a' in s.values
Out[32]: True
You can try this to check a particular value 'x' in a particular column named 'id'
if x in df['id'].values
How do i check if a dataframe column contains lists?
How to Check if Any Value in a List Is in either of Two Columns With Pandas
Pandas question - check if the content of a cell is contained in another cell
How to check if -any- item from a list of strings is in a Pandas series?
I have a script that runs nightly extracting data from an API and cleaning it up into a dataframe. It has worked fine up until today when all of a sudden is failing with the following error:
TypeError: unhashable type: 'list'
The line of code that is throwing that error is
final_df.drop_duplicates(inplace=True)
A check on stack overflow says that lists in dataframes arent hashable hence the error. How can i find the offending column (or columns) in my dataframe and fix the error?
I am loading a csv as a pandas dataframe.
The headers of the csv are: fname,lname,linkedin_url,work_email,personal_email,cell_phone,company_name,job_title,company_website,industry,location,gender,birth_day
I have a list of emails that I want to filter the dataframe with and keep the records that are in the list. I figured out how to do it based on one column using this:
datafilters = df['work_email'].isin(FilterThese)
How do I adjust the above so it checks if the values in the list "FilterThese" exist in either 'work_email' or 'personal_email'?
Thank you!