我的資料框如下所示:
id text labels
0 447 glutamine synthetase [protein]
1 447 GS [protein]
2 447 hepatoma [indication]
3 447 NaN NaN
4 442 Metachromatic [indication]
我想轉換資料框并創建兩個名為proteinsandindications的新列,其中包含textwhen labelsis protein 或相同 id 的指示。
想要的輸出
id protein indication
0 447 glutamine synthetase, GS hepatoma
0 442 NaN Metachromatic
有人可以幫助如何做到這一點嗎?
uj5u.com熱心網友回復:
df.explode與Groupby.agg和一起使用df.pivot:
In [417]: out = df.explode('labels').groupby(['id', 'labels'])['text'].agg(','.join).reset_index().pivot('id', 'labels').reset_index().droplevel(0, axis=1).rename_axis(None, axis=1)
In [423]: out.columns = ['id', 'indication', 'protein']
In [424]: out
Out[424]:
id indication protein
0 442 Metachromatic NaN
1 447 hepatoma glutamine synthetase,GS
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474520.html
下一篇:根據之前的標記除以列中的值
