本文介绍了在 pandas 中对齐DataFrame,以便数据在行中匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对齐我的数据

我目前这样子:

Yan     TNSeq   Kato    Eco-GeneOrth    Essential

accA    accA    accA        accA        accA
accB    accB    accB        accB        accB
accC    accC    accC        accC        accC
accD    accD    accD        accD        accD
aceF    acpP    acpP        alaS        aceF
acpP    acpS    acpS        argA        acpP
acpS    adk     adk         argB        acpS

这是我想要的:

Yan     TNSeq   Kato    Eco-GeneOrth    Essential

accA    accA    accA        accA        accA
accB    accB    accB        accB        accB
accC    accC    accC        accC        accC
accD    accD    accD        accD        accD
aceF    NaN     NaN         Nan         aceF
acpP    NaN     Nan         acpP        acpP
NaN     acpS    NaN         NaN         acpS

我尝试过重新索引和排序,但是没有运气

I've tried with reindex and sort, but no luck

我一无所知

基本上我想要的是将前4列与Essential列对齐或排序,以使行中的数据匹配.

Basically what I want is to align or sort the first 4 columns with the Essential column, so that the data in the rows matches.

推荐答案

更新:

In [120]: df[df.apply(lambda x: x['Essential'] == x, axis=1)]
Out[120]:
    Yan TNSeq  Kato Eco-GeneOrth Essential
0  accA  accA  accA         accA      accA
1  accB  accB  accB         accB      accB
2  accC  accC  accC         accC      accC
3  accD  accD  accD         accD      accD
4  aceF   NaN   NaN          NaN      aceF
5  acpP   NaN   NaN          NaN      acpP
6  acpS   NaN   NaN          NaN      acpS

尝试一下:

In [86]: df[df.apply(lambda x: x[0] == x, axis=1)]
Out[86]:
    Yan TNSeq  Kato Eco-GeneOrth Essential
0  accA  accA  accA         accA      accA
1  accB  accB  accB         accB      accB
2  accC  accC  accC         accC      accC
3  accD  accD  accD         accD      accD
4  aceF   NaN   NaN          NaN      aceF
5  acpP   NaN   NaN          NaN      acpP
6  acpS   NaN   NaN          NaN      acpS

数据:

In [87]: df
Out[87]:
    Yan TNSeq  Kato Eco-GeneOrth Essential
0  accA  accA  accA         accA      accA
1  accB  accB  accB         accB      accB
2  accC  accC  accC         accC      accC
3  accD  accD  accD         accD      accD
4  aceF  acpP  acpP         alaS      aceF
5  acpP  acpS  acpS         argA      acpP
6  acpS   adk   adk         argB      acpS

这篇关于在 pandas 中对齐DataFrame,以便数据在行中匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:22