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

501
Visualizações
Overlapping keys in dictionary when Using .replace() method on pandas dataframe

I want to replace some values in a column of a dataframe using a dictionary that maps the old codes to the new codes.

di = dict( { "myVar": {11:0, 204:11} } )
mydata.replace( to_replace = di, inplace = True )

But some of the new codes and old codes overlap. When using the .replace method of the dataframe I encounter the error 'Replacement not allowed with overlapping keys and values'

My current workaround is to replace replace the offending keys manually and then apply the dictionary to the remaining non-overlapping cases.

mydata.loc[ mydata.myVar == 11, "myVar" ] = 0 
di = dict( { "myVar": {204:11} } )
mydata.replace( to_replace = di, inplace = True )

Is there a more compact way to do this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I found an answer here that uses the .map method on a series in conjunction with a dictionary. Here's an example recoding dictionary with overlapping keys and values.

import pandas as pd
>>> df = pd.DataFrame( [1,2,3,4,1], columns = ['Var'] )
>>> df
   Var
0    1
1    2
2    3
3    4
4    1
>>> dict = {1:2, 2:3, 3:1, 4:3}
>>> df.Var.map( dict )
0    2
1    3
2    1
3    3
4    2
Name: Var, dtype: int64

UPDATE:

With map, every value in the original series must be mapped to a new value. If the mapping dictionary does not contain all the values of the original column, the unmapped values are mapped to NaN.

>>> df = pd.DataFrame( [1,2,3,4,1], columns = ['Var'] )
>>> dict = {1:2, 2:3, 3:1}
>>> df.Var.map( dict )
0    2.0
1    3.0
2    1.0
3    NaN
4    2.0
Name: Var, dtype: float64
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