0

I'm having data frame like this:

cust_id   s1   s2 
  1      100   0
  2      100   1
  3      90    1
  4      85    0
  5      90    1

and I'm trying to get the following output:

 s1    s1(Count)  s2(sum)
 100     2          1
 90      2          2
 85      1          0

I tried to use groupby and size/sum (like in here: Pandas: Create a new Data Frame using multiple GroupBy results):

e.g., abc = df.groupby(by="s1")["s1","s2"].sum() ## I don't know how to create count for s1 and sum for s2

Your help will be appreciated!

Community
  • 1
  • 1
staove7
  • 560
  • 5
  • 18
  • That's explained quite well in the documentation: [Applying different functions to dataframe columns](http://pandas.pydata.org/pandas-docs/stable/groupby.html#applying-different-functions-to-dataframe-columns). – IanS Jan 18 '17 at 14:00
  • my problem is that I'm changing the data frame dimensions (and I can't do it without implying the _"agg"_ functions (sum and count). It has to happened together. Can you please clarify the right way of doing it? Thanks! – staove7 Jan 18 '17 at 14:09
  • 1
    Try this: `df.groupby(by="s1")["s2"].agg(['size', 'sum'])` – IanS Jan 18 '17 at 14:11
  • Thank you very much. When I'm looking on it, it seems so intuitive :) – staove7 Jan 18 '17 at 14:15

0 Answers0