Pandas core groupby DataFrameGroupBy function any() in Python
The Pandas core groupby DataFrameGroupBy function any() used to determine whether any group values are truthfull. It returns either DataFrame or Series of boolean values where a value is True if any elements of group are True, otherwise False.
Syntax
 1 DataFrameGroupBy.any(skipna = True)
skipna : It is a boolean value that specifies whether to ignore nan value while determining truthfullness.
any() function
 1 import pandas as pd
 2 
 3 df = pd.DataFrame({'A' : ['A1', 'A2', 'A1', 'A2',
 4                           'A1', 'A3'],
 5                    'B' : [1, 6, 4, 3, 3, 0],
 6                    'C' : [1, 2.5, 4.4, 9.1, 7.2, 0]})
 7                    
 8 grouped = df.groupby('A')
 9 res = grouped.any()
 10 
 11 print('Does any groups are truthfull :')
 12 print(res)
In the above example, a DataFrame object is created by passing a dictionary object. A DataFrameGroupBy function any() is called that determine whether any group values are truthfull. The result is assign to the variable that will be printed on console.
Output
 1 Does any groups are truthfull :
 2         B      C
 3 A
 4 A1   True   True
 5 A2   True   True
 6 A3  False  False

any() with Series

any() with Series
 1 import pandas as pd
 2 
 3 ser = pd.Series([1, 6, 4, 3, 3, 0],
 4                 index = ['A1', 'A2', 'A1', 'A2',
 5                           'A1', 'A3'])
 6                    
 7 grouped = ser.groupby(ser.index)
 8 res = grouped.any()
 9 
 10 print('Does any groups are truthfull :')
 11 print(res)
In the above example, a Series object is created by passing an array and index. A SeriesGroupBy funtion any() is called that determine whether any group values are truthfull. The result is assign to the variable that will be printed on console.
Output
 1 Does any groups are truthfull :
 2 A1     True
 3 A2     True
 4 A3    False
 5 dtype: bool
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us