Reputation: 3379
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 len(set(x)==1)
the best way to determine if all items in the group are equal?
Upvotes: 3
Views: 5922
Reputation: 4418
SImilar to Jansen's answer, but doesn't build the set.
df.groupby(level=0).agg(lambda s:np.unique(s).size==1)
Value1 Value2
A True False
B False False
C True False
Upvotes: 3
Reputation: 42885
df.groupby(level=0).agg(lambda x: len(set(x)) == 1)
Value1 Value2
A True False
B False False
C True False
Upvotes: 5