Python Forum
Python group by and aggregate
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python group by and aggregate
#1
I have a dataframe with two columns ID and labels. Labels can only be 0 or 1.

The code below generates such a dataframe
data = [[10105, 1], [10105, 1], [10105, 0], [20205, 0], [20205, 0], [20205, 1], [20205, 1]] 

test=pd.DataFrame(data,columns=["ID","label"])

test
      ID  label
0  10105      1
1  10105      1
2  10105      0
3  20205      0
4  20205      0
5  20205      1
6  20205      1
I would like to get some statistics about the labels once data is grouped by ID.

The

test.groupby('ID')
will group the entries by ID but then I want to see how many entries with the ID 10105 have a label of 1 and how many have a label of a 0. Also I would like to calculate the percentage of 0s. That would be then the ideal output

ID 10105, label1: 2, label0: 1, Percantage (label0/(label1+label0)): 1/3
ID 20205, label1: 2, label0: 2, Percantage (label0/(label1+label0)): 2/4
I think python has a way to aggregate results but at the same time I need a way to make calculations between the labels of a specific ID.

Can you please help me?

I would like to thank you in advance for your reply.

Regards Alex
Reply
#2
Hi Alex,
what i sometimes do if i would like to know what methods can be used on an object is this:
[func for func in dir(test.groupby('ID')) if func[0] != '_']
This gives you a long list of methods and/or properties.

In your case test.groupby('ID').sum() might be interesting.
Look also into .count() .groups .nunique()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 75 Yesterday, 01:16 PM
Last Post: ann23fr
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,693 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  group by create pivot table python dawid294 1 1,258 Jun-22-2022, 06:13 PM
Last Post: Larz60+
  df column aggregate and group by multiple columns SriRajesh 0 976 May-06-2022, 02:26 PM
Last Post: SriRajesh
  Aggregate krisrajz 1 30,735 Apr-11-2021, 08:48 PM
Last Post: Yoriz
  Python list - group by dict key karthidec 2 9,331 Nov-25-2019, 06:58 AM
Last Post: buran
  How to aggregate rows with same column Leoni 1 2,499 Jul-08-2018, 08:33 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020