我有一张这样的桌子:
BatchNo | Replication | Bioreactor | Centrifuge | Red | Amber | Green
------------|-------------|------------|------------|-----|-------|-------
A1-1 | 1 | 1 | 1 | 1 | 0 | 0
A1-2 | 0 | 0 | 0 | 0 | 0 | 1
A1-3 | 1 | 0 | 1 | 0 | 1 | 0 我需要采取的步骤:创建一个包含列的新列阶段--复制、生物反应器、离心机等类别,并将它们的值转换为填充列状态。然后,根据红色、琥珀或绿色字段中某个值的出现情况,填充另一个列数据。
BatchNo | Stage | Status | RAG
--------|-------------|--------|------
A1-1 | Replication | 1 | Red
A1-1 | Bioreactor | 1 | Red
A1-1 | Centrifuge | 1 | Red 有什么办法吗?我需要做两个转换吗?干杯
发布于 2019-05-15 15:20:31
在您的示例中,为RAG分配新值,为melt分配df
s=df.loc[:,'Red':]
df['RAG']=s.dot(s.columns)
df=df.melt(['BatchNo','RAG'])
df=df.loc[df.value.eq(1)&df.RAG.ne(df.variable)].copy()
df
Out[57]:
BatchNo RAG variable value
0 A1-1 Red Replication 1
2 A1-3 Amber Replication 1
3 A1-1 Red Bioreactor 1
6 A1-1 Red Centrifuge 1
8 A1-3 Amber Centrifuge 1发布于 2019-05-15 16:18:51
设置index
d = df.set_index('BatchNo')将类别放置到stack中的部分
loc用于过滤等于1的值。
cats = np.array(['Replication', 'Bioreactor', 'Centrifuge'])
cat = d[cats].rename_axis('Stage', 1).stack().loc[lambda x: x == 1].to_frame('Status')这些被认为是“一火如荼”
因此,我可以用idxmax获取第一批
rag = d[np.array(['Red', 'Amber', 'Green'])].idxmax(1).rename('RAG')在适当地重命名轴和列之后..。
我应该可以join了
cat.join(rag).reset_index()
BatchNo Stage Status RAG
0 A1-1 Replication 1 Red
1 A1-1 Bioreactor 1 Red
2 A1-1 Centrifuge 1 Red
3 A1-3 Replication 1 Amber
4 A1-3 Centrifuge 1 Amberhttps://stackoverflow.com/questions/56152455
复制相似问题