0

I have a sample data-frame and I want to filter a few columns based on the same value but I want to avoid duplicating line to do this like:

df = df.loc[df['a'] == 7]
df = df.loc[df['b'] == 7]

I am looking for a more clean way to write this since I will have many columns with the same treatments. Here's my attempt:

df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 7, 9]]), columns=['a', 'b', 'c']) 
my_cols = ['a','b']
print(df.loc[df[my_cols]==7])

But I get the following error:

Cannot index with multidimensional key

Does anyone know if is it possible to make this type of filter?

Thanks!

briba
  • 2,857
  • 2
  • 31
  • 59
  • https://stackoverflow.com/questions/42711186/python-pandas-how-to-filter-multiple-columns-by-one-value – PV8 Sep 23 '19 at 10:24
  • 1
    @PV8 - Can you find some better dupe? Because `iloc` here is not encessary. – jezrael Sep 23 '19 at 10:28

1 Answers1

3

Because df[my_cols]==7 return boolean DataFrame is necessary add DataFrame.any:

print(df[(df[my_cols]==7).any(axis=1)])
   a  b  c
2  7  7  9

print (df[my_cols]==7)
       a      b
0  False  False
1  False  False
2   True   True
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252