Created
June 1, 2017 18:21
-
-
Save lekhanh1234/67755d7e287c138beb229bb35e6e400a to your computer and use it in GitHub Desktop.
lab_aggregation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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