I have a Pandas DataFrame where certain rows have the same values in similar columns. I want to create a boolean mask that is True when all these columns have the same values for the specific row. I want to dynamically pass a list of columns to check. Example:
A | B | C | Mask
1 | 1 | 1 | True
2 | 2 | 3 | False
4 | 4 | 4 | True
The mask should be returned by my same_values function that was passed the DataFrame and a list of columns. For example
same_values(data, ['A', 'B', 'C'])
Without the dynamic pass I can do it like this:
data[(data['A']==data['B'])&(data['A']==data['C'])]
I could dynamically iterate over all the columns and compare them with the first passed column but this seems inefficient. Who has a better solution?