Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save karpanGit/992f1fd6aa630137271a341054e46901 to your computer and use it in GitHub Desktop.

Select an option

Save karpanGit/992f1fd6aa630137271a341054e46901 to your computer and use it in GitHub Desktop.
understand pandas groupby/apply arguments group_keys and include_groups
import pandas as pd
df = pd.DataFrame(
{
"A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
"B": ["one", "one", "two", "three", "two", "two", "one", "three"],
"C": np.random.randn(8),
"D": np.random.randn(8),
}
)
df.groupby('A', group_keys=True).apply(lambda x: x, include_groups=True) # group_keys matters when using apply
# A B C D
# A
# bar 1 bar one -1.583891 -1.708604
# 3 bar three 0.352257 -0.088550
# 5 bar two 0.857819 -0.480496
# foo 0 foo one -0.290060 0.856008
# 2 foo two 1.192185 -0.243608
# 4 foo two 0.772302 -0.791737
# 6 foo one -0.446585 1.177601
# 7 foo three -1.776370 1.541010
df.groupby('A', group_keys=True).apply(lambda x: x, include_groups=False)
# B C D
# A
# bar 1 one -1.583891 -1.708604
# 3 three 0.352257 -0.088550
# 5 two 0.857819 -0.480496
# foo 0 one -0.290060 0.856008
# 2 two 1.192185 -0.243608
# 4 two 0.772302 -0.791737
# 6 one -0.446585 1.177601
# 7 three -1.776370 1.541010
df.groupby('A', group_keys=False).apply(lambda x: x, include_groups=True)
# A B C D
# 0 foo one -0.290060 0.856008
# 1 bar one -1.583891 -1.708604
# 2 foo two 1.192185 -0.243608
# 3 bar three 0.352257 -0.088550
# 4 foo two 0.772302 -0.791737
# 5 bar two 0.857819 -0.480496
# 6 foo one -0.446585 1.177601
# 7 foo three -1.776370 1.541010
df.groupby('A', group_keys=False).apply(lambda x: x, include_groups=False)
# B C D
# 0 one -0.290060 0.856008
# 1 one -1.583891 -1.708604
# 2 two 1.192185 -0.243608
# 3 three 0.352257 -0.088550
# 4 two 0.772302 -0.791737
# 5 two 0.857819 -0.480496
# 6 one -0.446585 1.177601
# 7 three -1.776370 1.541010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment