Python – Pandas: How to print groupby values

Pandas: How to print groupby values… here is a solution to the problem.

Pandas: How to print groupby values

I have the following dataset from Table_Record:

Seg_ID  Lock_ID  Code
111     100      1
222     121      2
333     341      2
444     100      1
555     100      1
666     341      2
777     554      4
888     332      5

I’m using a sql query to find duplicate Lock_ID Seg_IDs:

Select Code,Lock_ID,Seg_ID from Table_Record group by Code, Lock_ID;

Seg_ID  Lock_ID  Code
111     100      1
444     100      1
555     100      1
222     121      2
333     341      2
666     341      2
777     554      4
888     332      5

How can I use Pandas to achieve the same effect?

Excepted O/P from Pandas is:

For example.

Seg_ID (111,444,555) has Lock_id (1).
Seg_ID (222,333,666) has Lock_ID (2).

Solution

First, by filtering only >duplicated Get all the codes values and press isin :

codes = df.loc[df.duplicated(['Lock_ID']), 'Code'].unique()

df1 = df[df['Code'].isin(codes)]
print (df1)
   Seg_ID  Lock_ID  Code
0     111      100     1
1     222      121     2
2     333      341     2
3     444      100     1
4     555      100     1
5     666      341     2

Then groupby is used f-strings:

for k, v in df1.groupby(['Code'])['Seg_ID']:
    print (f'Seg_ID {tuple(v)} has Code ({k})')

Seg_ID (111, 444, 555) has Code (1)
Seg_ID (222, 333, 666) has Code (2)

If you want output like DataFrame, use apply and tuple:

df2 = df1.groupby(['Code'])['Seg_ID'].apply(tuple).reset_index()
print (df2)
   Code           Seg_ID
0     1  (111, 444, 555)
1     2  (222, 333, 666)

Related Problems and Solutions