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.
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 .
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.
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)
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