Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding values in pandas series - Python3

i have this excruciatingly annoying problem (i'm quite new to python)

df=pd.DataFrame[{'col1':['1','2','3','4']}]

col1=df['col1']

Why does col1[1] in col1 return False?

like image 795
mystery man Avatar asked Mar 09 '17 14:03

mystery man


People also ask

How do you find a string in a series in python?

find() method is used to search a substring in each string present in a series. If the string is found, it returns the lowest index of its occurrence. If string is not found, it will return -1. Start and end points can also be passed to search a specific part of string for the passed character or substring.

Can we analyze data in Pandas with series?

Series is one dimensional(1-D) array defined in pandas that can be used to store any data type. Here, Data can be: A Scalar value which can be integerValue, string. A Python Dictionary which can be Key, Value pair.


2 Answers

For check values use boolean indexing:

#get value where index is 1
print (col1[1])
2 
#more common with loc
print (col1.loc[1])
2

print (col1 == '2')
0    False
1     True
2    False
3    False
Name: col1, dtype: bool

And if need get rows:

print (col1[col1 == '2'])
1    2
Name: col1, dtype: object

For check multiple values with or:

print (col1.isin(['2', '4']))
0    False
1     True
2    False
3     True
Name: col1, dtype: bool 

print (col1[col1.isin(['2', '4'])])
1    2
3    4
Name: col1, dtype: object

And something about in for testing membership docs:

Using the Python in operator on a Series tests for membership in the index, not membership among the values.

If this behavior is surprising, keep in mind that using in on a Python dictionary tests keys, not values, and Series are dict-like. To test for membership in the values, use the method isin():

For DataFrames, likewise, in applies to the column axis, testing for membership in the list of column names.

#1 is in index
print (1 in col1)
True

#5 is not in index
print (5 in col1)
False

#string 2 is not in index
print ('2' in col1)
False

#number 2 is in index
print (2 in col1)
True

You try to find string 2 in index values:

print (col1[1])
2

print (type(col1[1]))
<class 'str'>

print (col1[1] in col1)
False
like image 53
jezrael Avatar answered Sep 19 '22 22:09

jezrael


I might be missing something, and this is years later, but as I read the question, you are trying to get the in keyword to work on your panda series? So probably want to do:

col1[1] in col1.values 

Because as mentioned above, pandas is looking through the index, and you need to specifically ask it to look at the values of the series, not the index.

like image 23
Daniel81 Avatar answered Sep 16 '22 22:09

Daniel81