Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a series as input, how can I find rows with matching values in a pandas dataframe? e.g. df.loc[series]?

I have a DataFrame df and a series s matching the columns in df. I would like to find all rows in df that have the same values as s. I should probably mention that the columns can change, but s will always be a row within df.

df = pd.DataFrame.from_dict({'a': {0: 0.015, 1: 0.02, 2: 0.025, 3: 0.015},
 'b': {0: True, 1: True, 2: True, 3: True},
 'c': {0: 'foo', 1: 'foo', 2: 'foo', 3: 'foo'},
 'd': {0: 1, 1: 1, 2: 1, 3: 1}})
s = df.loc[0]

The expected result in this case would be the return value of df.loc[[0,3]]. Alternatively, the index of the matching rows would do.

Of course, if the columns were the same everytime df.loc[df['a']==s['a'], df['b']==s['b'], ...] would work just fine. Maybe there is a way of generating the term within df.loc[ ] by means of some sort of comprehension?

I am imagining there must be a pythonic/pandas way of doing this.

like image 619
m3ph Avatar asked Nov 28 '25 00:11

m3ph


1 Answers

Compare rows by DataFrame.eq and then test if all Trues per rows by DataFrame.all and last filter by boolean indexing:

df = df[df.eq(s).all(axis=1)]
print (df)
       a     b    c  d
0  0.015  True  foo  1
3  0.015  True  foo  1

Details:

print (df.eq(s))
       a     b     c     d
0   True  True  True  True
1  False  True  True  True
2  False  True  True  True
3   True  True  True  True

print (df.eq(s).all(axis=1))
0     True
1    False
2    False
3     True
dtype: bool
like image 192
jezrael Avatar answered Nov 29 '25 12:11

jezrael