简体   繁体   中英

How to assign child objects to parent objects using pandas python

I have a data frame in pandas that looks like the following:

df =

         Image_Number     Parent_Object      Child_Object
             1                 1                  1
             1                 1                  2
             1                 1                  3
             1                 1                  4
             1                 2                  5
             1                 1                  6
             1                 2                  7
             1                 2                  8
             1                 3                  9                                   
             1                 3                  10
             1                 3                  11
             1                 3                  12
             2                 1                  13                                   
             2                 1                  14
             2                 1                  15
             2                 1                  16
             2                 2                  17
             2                 2                  18                
             2                 2                  19
             2                 3                  20
             2                 3                  21
             2                 3                  22                                                                       
             2                 2                  23
             2                 3                  24
             2                 3                  25
             3                 1                  26
             3                 1                  27                                   
             3                 1                  28
             3                 2                  29
             3                 2                  30

How could I write something that would classify the child objects to the parent objects for each image?

It would be extremely helpful to get an output like the following:

Image_Number             Parent_Object       Number_of_Child_Objects
     1                         1                      5
     1                         2                      3
     1                         3                      4
     2                         1                      4
     2                         2                      3
     2                         3                      3
     3                         1                      3
     3                         2                      2

What you want to do is calculate something (counts) for different values (groups) of Image_Number and Parent_Object . This can be done with the groupby method (see here for the docs: http://pandas.pydata.org/pandas-docs/stable/groupby.html

In your case:

df.groupby(by=['Image_Number', 'Parent_Object']).count()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM