Skip to content

Commit

Permalink
Merge branch 'main' into custom_func
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonbb committed Apr 18, 2024
2 parents c845bf8 + ac4032f commit 9b0dcd7
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/nested_pandas/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def count_nested(df, nested, by=None, join=True) -> NestedFrame:
"""

if by is None:
counts = df[nested].apply(lambda x: len(x)).rename(f"n_{nested}")
# to_flat() is faster than direct apply in this case
counts = df[nested].nest.to_flat().groupby(level=0).apply(lambda x: len(x)).rename(f"n_{nested}")
else:
counts = df[nested].apply(lambda x: x[by].value_counts())
counts = counts.rename(columns={colname: f"n_{nested}_{colname}" for colname in counts.columns})
Expand Down

0 comments on commit 9b0dcd7

Please sign in to comment.