Changing a value with the pandas library

1

I am opening a .csv file with the pandas library, however I am informed at the time of opening this file that a particular column presents values of different types. I know the character "/" was used in this file to denote missing data, this is very likely to be the problem. My question is, how to replace the "/" character with another value?

    
asked by anonymous 11.02.2018 / 15:52

2 answers

1

If the desired data type is numeric, you can, after opening the file, do this:

df['coluna']=pd.to_numeric(df['coluna'], errors='coerce')

So, you already convert the existing values to number and the '/' to NaN. Then if you want a number in place of NaN, you can use the fillna () function described by Claudio Gonçalves Filho.

    
14.02.2018 / 18:51
0

Good afternoon Mauricio, all right?

I think if the pandas are identifying '/' as a character, you can use the 'apply' function.

novodf = antigodf.apply(lambda x: x.replace('/','0'))

In this way you would replace '/' with '0'

If you want to replace '/' with a value of NaN, to use functions related to null numbers of pandas, import the numpy library and replace the '0' of apply with np.nan, thus:

import numpy as np
novodf = antigodf.apply(lambda x: x.replace('/',np.nan))

If you want to replace NaN, you can use the fillna() function by passing the value that will replace NaN

novodf.fillna(0)

This command would replace the NaN values by 0.

Follows a link from Paulo Vasconcellos's website with great pandas and data science tips: PauloVasconcellos

I hope I have helped.

Thank you.

Claudio.

    
14.02.2018 / 18:28