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

199
Visualizações
replace part of the string in pandas data frame

I have pandas data frame in which I need to replace one part of the vale with another value

for Example. I have

HF - Antartica
HF - America
HF - Asia

out of which I'd like to replace ony the HF - part thus the result would be

Hi Funny Antartica
Hi Funny America
Hi Funny Asia

I have tried pd.replace() but it doesnt work as I need only one part of the string replaced, rather than the entire string

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

0

It seems you need Series.replace:

print (df)
              val
0  HF - Antartica
1    HF - America
2       HF - Asia

print (df.val.replace({'HF -':'Hi'}, regex=True))
0    Hi Antartica
1      Hi America
2         Hi Asia
Name: val, dtype: object

Similar solution with str.replace:

print (df.val.str.replace('HF -', 'Hi'))
0    Hi Antartica
1      Hi America
2         Hi Asia
Name: val, dtype: object
over 4 years ago · Santiago Trujillo Relatório

0

To add to @jezrael's answer, you need to include regex=True otherwise it would match directly. Also, here it replaces the values across all columns in the data frame. If you don't intend this, you could filter to a column and then replace. For replacing across all values in the data frame, try:

df.replace('HF', 'Hi Funny', regex=True)

You could also provide a list based patterns and replacement values. The complete set of options are provided in the documentation here.

So if the data frame is:

>df = pd.DataFrame({'Column': ['HF - Antartica', 'HF - America', 'HF - Asia']})
>df.replace('HF', 'Hi Funny', regex=True)

should print:

                 Column
0  Hi Funny - Antartica
1    Hi Funny - America
2       Hi Funny - Asia
over 4 years ago · Santiago Trujillo Relatório

0

I would like to share one more thing that is very much important, you can replace full stop with space ". " with "." normal full stop

df['label']=df.label.replace({"\. ": "."},regex=True)
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