简体   繁体   中英

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.

Compare rows by DataFrame.eq and then test if all True s 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM