Find All Indices/instances Of All Repeating Patterns Across Columns And Rows Of Pandas Dataframe
Suppose I have a simple pandas dataframe df as so: | name | car | |----|-----------|-------| | 0 | 'bob' | 'b' | | 1 | 'bob' | 'c' | | 2 | 'fox' | 'b
Solution 1:
Use boolean indexing
with Series.isin
instead second and third conditions:
df1 = df[(df.name == 'bob') & df.car.isin(['b','c'])]
print (df1)
name car
0 bob b
1 bob c
8 bob b
9 bob c
10 bob b
11 bob c
If need index values:
out_idx = df.index[(df.name == 'bob') & df.car.isin(['b','c'])]
Or:
out_idx = df[(df.name == 'bob') & df.car.isin(['b','c'])].index
Your solution is possible with |
(bitwise OR) instead second &
and also added one ()
:
df1 = df[(df.name == 'bob') & ((df.car == 'b') | (df.car == 'c'))]
Post a Comment for "Find All Indices/instances Of All Repeating Patterns Across Columns And Rows Of Pandas Dataframe"