1

Concretely, say I have a DataFrame like this:

appid  mac_id  count

1      a        1 

2      b        1

2      c        1

3      d        1

3      e        1

And I also have a :

mac_list = ['b', 'd', 'e']

I want to group this data frame on appid and for every group filter mac_id if it's in mac_list. Last, sum(count) for every group.

for this DataFrame the result is:

appid   count

1         0

2         1

3         2

How can I do this with Pandas?

1 Answer 1

7
>>> df = pd.DataFrame({"appid": [1,2,2,3,3], "mac_id": ['a', 'b', 'c', 'd', 'e'], "count": [1,1,1,1,1]})
>>> summer = lambda x: x[x["mac_id"].isin(mac_list)].sum()
>>> df.groupby("appid").apply(summer)["count"]
  18 
appid
1        0
2        1
3        2
Name: count, dtype: object
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.