Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

append ids when specific column is the same

For example I have this table

| ID       | VALUE        |

| -------- | -------------- |
| 1    | row24           |
| 2   | row24            |
| 3    | row1            |
| 4   | row15            |
| 5    | row16           |
| 6   | row17            |
| 8   | row24            |
| 7   | row17            |
| 9   | row19            |

Output should be:

| ID       | VALUE        |

| -------- | -------------- |
| [1,2,8]    | row24           |

| 3    | row1            |
| 4   | row15            |
| 5    | row16           |
| [6,7]   | row17         |
| 9   | row19            |

I think maybe groupby in pandas is a solution, i tried some but it didnt work…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If need combination lists and scalars use GroupBy.agg with lambda function:

df =  (df.groupby('VALUE', sort=False)['ID']
        .agg(lambda x: list(x) if len(x) > 1 else x)
        .reset_index(name='IDS'))
print (df)
   VALUE        IDS
0  row24  [1, 2, 8]
1   row1          3
2  row15          4
3  row16          5
4  row17     [6, 7]
5  row19          9

Because ig aggregate only list get oalso one element lists:

df =  (df.groupby('VALUE', sort=False)['ID']
        .agg(list)
        .reset_index(name='IDS'))
print (df)
   VALUE        IDS
0  row24  [1, 2, 8]
1   row1        [3]
2  row15        [4]
3  row16        [5]
4  row17     [6, 7]
5  row19        [9]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading