Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

358
Visualizações
Selecting column with smallest value in pandas dataframe

I have a pandas dataframe with this structure:

df_test

V       |  A    | B   |  C      |  D
-10     |  nan  | nan |  nan    |  nan  
-9.9    |  10   | 1   |  -2200  |  100
-9.8    |  11   | 2   |  -2211  |  1

I would like to add a new column minimum_difference containing the column name with the smallest absolute value for that specific row (ignoring V), like this:

V       |  A    | B   |  C      |  D    | minimum_difference
-10     |  nan  | nan |  nan    |  nan  | nan
-9.9    |  10   | 1   |  -2200  |  100  | B
-9.8    |  11   | 2   |  -2211  |  1    | D
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can use df.drop to skip the V column, and df.abs to convert the dataframe's values to their absolute equivalents, and use df.idxmin with axis=1 to find the index (which will be a column name) of the smallest (absolute) value:

df_test['minimum_difference'] = df_test.drop('V', axis=1).abs().idxmin(axis=1)

Output:

>>> df
      V     A    B       C      D minimum_difference
0 -10.0   NaN  NaN     NaN    NaN                NaN
1  -9.9  10.0  1.0 -2200.0  100.0                  B
2  -9.8  11.0  2.0 -2211.0    1.0                  D
over 4 years ago · Santiago Trujillo Relatório

0

You can check if a row contains NaN, then put NaN, otherwise calculate the min of the row and put the index.

In [101]: import numpy as np

In [102]: df['minimum_difference'] = np.where(df.isna().any(1), np.nan, df.abs().idxmin(1))

In [103]: df
Out[103]: 
      V     A    B       C      D minimum_difference
0 -10.0   NaN  NaN     NaN    NaN                NaN
1  -9.9  10.0  1.0 -2200.0  100.0                  B
2  -9.8  11.0  2.0 -2211.0    1.0                  D
over 4 years ago · Santiago Trujillo Relatório

0

Here I am sharing Mayank's snippet with little update.

In [84]: df['minimum_difference'] = df.ix[:, 1:].abs().idxmin(1)

In [85]: df
Out[85]: 
      V     A    B       C      D minimum_difference
0 -10.0   NaN  NaN     NaN    NaN                  NaN
1  -9.9  10.0  1.0 -2200.0  100.0                  B
2  -9.8  11.0  2.0 -2211.0  200.0                  B
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda