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!