Is there any way to replace one number with another in a Dataframe?

Asked

Viewed 76 times

0

I have a dataset to analyze, and it in some columns contains values that reference others. I would like to know if there is any way to replace one numerical value with another.

I tried to do this task with the replace but it didn’t work out.

I leave an image with the code, and the link to the dataset I’m analyzing.

inserir a descrição da imagem aqui

  • 1

    Please put the code in writing in the body of the question and press Ctrl + k with the selected code

  • 1

    When asking or answering do not post code as image, code should be placed as text: Reference How NOT to ask questions manual: Post code as image

  • I have already managed to solve the problem! I am sorry to have put the question like this, it will not happen again!

1 answer

1


replace has to be done with a dictionary.

df['variavel'].replace({old_value : new_value})

Replicable example:

import pandas as pd

df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                'B': [5, 6, 7, 8, 9],
                'C': ['a', 'b', 'c', 'd', 'e']})
#trocando 1 por 214
df['A'] = df['A'].replace({1:214})
df
     A  B  C
0    0  5  a
1  214  6  b
2    2  7  c
3    3  8  d
4    4  9  e
  • I’ll try it like you told me, thank you!

  • Okay. See the documentation for more details and alternatives in this method: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.replace.html

  • Still doesn’t work, gives me the same mistake.

  • This is the command. I will edit my reply to include a replicable example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.