import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three'],
'C': [1, 2, 3, 4, 5, 6, 7, 8],
'D': [10, 20, 30, 40, 50, 60, 70, 80]})
# group the DataFrame by column A
grouped = df.groupby('A')
# define a custom function that selects the group with key 'foo'
def select_foo_group(group):
return group['A'].iloc[0] == 'foo'
# get the group with key 'foo'
foo_group = grouped.filter(select_foo_group)
print(foo_group)