5

Let's assume, there is a table like this:

Id | Type | Guid

I perform on such a table the following operation:

df = df.groupby('Id')

Now I would like to iterate through first n rows and for each specific Id as a list print all the corresponding entries from column Guid. Please, help me with a solution.

Server Khalilov
  • 408
  • 2
  • 5
  • 20
  • 2
    Something like `df.groupby('Id').head(10)`? – ayhan May 06 '17 at 23:07
  • How does it help me with iteration? – Server Khalilov May 06 '17 at 23:09
  • I'm left wondering if you want to iterate through the first `n` `Id`s... since you've grouped by `Id`? Or do you want to iterate through the first 10 rows within each group? This is a poorly formed question. Please read [***MCVE***](http://stackoverflow.com/help/mcve) to get a better idea of what we expect. This information only helps you get your question answered. – piRSquared May 06 '17 at 23:47
  • I am sorry that my question is not clear for you. However, I tried to explain it appropriately. Yes, your first assumption is correct. I want to iterate through the first `n` `Id`'s. – Server Khalilov May 07 '17 at 06:42

2 Answers2

9

I think I would do it like this:

Create some data for testing

df = pd.DataFrame({'Id':np.random.randint(1,10,100),'Type':np.random.choice(list('ABCD'),100),'Guid':np.random.randint(10000,99999,100)})

print(df.head()
   Id Type   Guid
0   2    A  89247
1   4    B  39262
2   3    C  45522
3   1    B  99724
4   4    C  51322

Choose n for number of records to return and groupby

n = 5
df_groups = df.groupby('Id')

Iterate through df_group with for loop and print

for name,group in df_groups:
    print('ID: ' + str(name))
    print(group.head(n))
    print("\n")

Output:

ID: 1
    Id Type   Guid
3    1    B  99724
5    1    B  74182
37   1    D  49219
47   1    B  81464
65   1    C  84925


ID: 2
    Id Type   Guid
0    2    A  89247
6    2    A  16499
7    2    A  79956
34   2    C  56393
40   2    A  49883
.
.
.

EDIT To print all the Guids in a list for each ID you can use the following:

for name,group in df_groups:
    print('ID: ' + str(name))
    print(group.Guid.tolist())
    print("\n")

Output:

ID: 1
[99724, 74182, 49219, 81464, 84925, 67834, 43275, 35743, 36478, 94662, 21183]


ID: 2
[89247, 16499, 79956, 56393, 49883, 97633, 11768, 14639, 88591, 31263, 98729]


ID: 3
[45522, 13971, 75882, 96489, 58414, 22051, 80304, 46144, 22481, 11278, 84622, 61145]


ID: 4
[39262, 51322, 76930, 83740, 60152, 90735, 42039, 22114, 76077, 83234, 96134, 93559, 87903, 98199, 76096, 64378]


ID: 5
[13444, 55762, 13206, 94768, 19665, 75761, 90755, 45737, 23506, 89345, 94912, 81200, 91868]
.
.
.
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Thanks! It looks like what I need. And if I want to print all `Guid`'s for each specific `ID`, is the following piece of code correct? `for name,group in df_groups:` `print('ID: ' + str(name))` `print(list(group['Guid']))` – Server Khalilov May 07 '17 at 06:47
  • @ServerKhalilov See the edits for what I think you want. – Scott Boston May 07 '17 at 19:02
  • Unfortunately, `head(20)` didn't do the work for me, so I got the complete amount of `id`'s with lists of `guid`'s attached to them. Thanks, anyway! – Server Khalilov May 09 '17 at 15:47
4

I like to use get_group for this. First you can pull out the keys:

In [11]: df
Out[11]:
   A  B
0  1  2
1  1  4
2  2  6
3  3  8

In [12]: g = df.groupby("A")

In [13]: g.groups.keys()
Out[13]: dict_keys([1, 2, 3])

You can iterate through the keys:

In [14]: for k in g.groups.keys():
             print(g.get_group(k))
             print("\n")
   A  B
0  1  2
1  1  4

   A  B
2  2  6

   A  B
3  3  8

To get the first n items of a DataFrame you can use head:

In [21]: df.head(3)  # or g.get_group(k).head(n)
Out[21]:
   A  B
0  1  2
1  1  4
2  2  6

Note: The groupby also has a head method which takes the first n of each group:

In [21]: g.head(1)
Out[21]:
   A  B
0  1  2
2  2  6
3  3  8
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535