Попробуйте:
df.groupby(['Gene name','Level'], as_index=False)['Cell type'].agg(', '.join)
Вывод:
| | Gene name | Level | Cell type |
|---:|:------------|:-------------|:----------------------------------------------------------------------------------------------------------------|
| 0 | CD99 | High | hematopoietic cells |
| 1 | CD99 | Low | adipocytes |
| 2 | CD99 | Medium | glandular cells |
| 3 | CD99 | Not detected | glandular cells , lymphoid tissue , adipocytes |
| 4 | ENPP4 | High | glandular cells |
| 5 | ENPP4 | Low | adipocytes , lymphoid tissue |
| 6 | ENPP4 | Medium | glandular cells , hematopoietic cells |
| 7 | M6PR | High | adipocytes , glandular cells , glandular cells , lymphoid tissue , hematopoietic cells |
Обновление добавлено по комментариям ниже:
(df.groupby(['Gene name','Level'], as_index=False)['Cell type']
.agg(','.join).set_index(['Gene name','Level'])['Cell type']
.unstack().reset_index())
Вывод:
| Gene name | High | Low | Medium | Not detected |
|:------------|:----------------------------------------------------------------------------------------------------------------|:---------------------------------------|:-------------------------------------------|:---------------------------------------------------------|
| CD99 | hematopoietic cells | adipocytes | glandular cells | glandular cells , lymphoid tissue , adipocytes |
| ENPP4 | glandular cells | adipocytes , lymphoid tissue | glandular cells , hematopoietic cells | nan |
| M6PR | adipocytes , glandular cells , glandular cells , lymphoid tissue , hematopoietic cells | nan | nan | nan |