Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if there exists a row with a certain column value in pandas dataframe

Very new to pandas.

Is there a way to check given a pandas dataframe, if there exists a row with a certain column value. Say I have a column 'Name' and I need to check for a certain name if it exists.

And once I do this, I will need to make a similar query, but with a bunch of values at a time. I read that there is 'isin', but I'm not sure how to use it. So I need to make a query such that I get all the rows which have 'Name' column matching to any of the values in a big array of names.

like image 800
AMM Avatar asked Apr 06 '14 14:04

AMM


People also ask

How do you check if a value exists in a data frame?

Use the in keyword to check if a value exists in a column of a DataFrame. Use the syntax value in pandas. DataFrame. column_name to determine if value exists in column_name of DataFrame .

How do you check if a value is in a series pandas?

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

import numpy as np
import pandas as pd
df = pd.DataFrame(data = np.arange(8).reshape(4,2), columns=['name', 'value'])

Result:

>>> df
   name  value
0     0      1
1     2      3
2     4      5
3     6      7
>>> any(df.name == 4)
True
>>> any(df.name == 5)
False

Second Part:

my_data = np.arange(8).reshape(4,2)
my_data[0,0] = 4

df = pd.DataFrame(data = my_data, columns=['name', 'value'])

Result:

>>> df.loc[df.name == 4]
   name  value
0     4      1
2     4      5

Update:

my_data = np.arange(8).reshape(4,2)
my_data[0,0] = 4

df = pd.DataFrame(data = my_data, index=['a', 'b', 'c', 'd'], columns=['name', 'value'])

Result:

>>> df.loc[df.name == 4]  # gives relevant rows
   name  value
a     4      1
c     4      5  
>>> df.loc[df.name == 4].index  # give "row names" of relevant rows
Index([u'a', u'c'], dtype=object)
like image 89
Akavall Avatar answered Oct 02 '22 18:10

Akavall