AJG519 AJG519 - 10 months ago 33
Python Question

Check if values within a group are equal in Pandas

I have something like the following dataframe:

>>> df=pd.DataFrame(index=['A','A','A','B','B','C','C','C'])
>>> df['Value1']=[1,1,1,2,1,4,4,4]
>>> df['Value2']=[1,2,3,4,5,6,7,8]
>>> df
Value1 Value2
A 1 1
A 1 2
A 1 3
B 2 4
B 1 5
C 4 6
C 4 7
C 4 8

The index of the dataframe represents 3 different groups. I want to check whether or not the column values within a group are all equal. My current methodology and desired output are the following:

>>> result_list=[]
>>> for Col in df.columns:
... result=df.groupby(level=0)[Col].apply(lambda x: len(set(x))==1)
... result_list.append(result)
>>> final=pd.concat(result_list,1)
>>> final
Value1 Value2
A True False
B False False
C True False

Is this the best approach to take? I'm wondering if it's possible to do this without having to loop through the columns in my dataframe.

Additionally is using
the best way to determine if all items in the group are equal?

df.groupby(level=0).agg(lambda x: len(set(x)) == 1)

  Value1 Value2
A   True  False
B  False  False
C   True  False