Search code examples
pythonpandassklearn-pandas

Replacing values in dataset


First sorry for this newbie question, but I'm learning for myself so its a little more challenging. I'm with a problem in replacing some negative values in one label of my dataset. In a nutshel, I want to replace those negative values with a default value. I tried doing the code below but didn't work.

dset['age'].replace(dset['age'] < 0 ,40)

Someone can help me? I know that changing it on CSV file is easier, but I'm trying to get used do pandas library.

dset['age'].replace(dset['age'] < 0 ,40)

tried too:

dset['age'] = dset['age'].replace(dset['age'] < 0 ,40)
dset = pd.read_csv('credit-data.csv')
dset['age'].replace(dset['age'] < 0 ,40)

Solution

  • dset.loc[dset['age'] < 0, 'age'] = 40