Skip to content

Instantly share code, notes, and snippets.

@lekhanh1234
Created June 1, 2017 18:21
Show Gist options
  • Select an option

  • Save lekhanh1234/67755d7e287c138beb229bb35e6e400a to your computer and use it in GitHub Desktop.

Select an option

Save lekhanh1234/67755d7e287c138beb229bb35e6e400a to your computer and use it in GitHub Desktop.
lab_aggregation
import pandas as pd
df = pd.read_excel("input.xlsx")
df = df.groupby(["A", "B"])["A"].count().reset_index(name="Count")
df_sum = df.groupby('B').sum().reset_index()
df1 = df.groupby('B').head(2)
df1 = df1.groupby('B')['A'].apply(lambda x: "%s" % ', '.join(x)).reset_index(name='Top')
df1.merge(df_sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment