Stacey
Stacey

Reputation: 5097

Replace specific dataframe columns values with blank

I have dataframe column (df) which I am trying to replace 999 values with blank. The head of the unadjusted column looks like:

Name: IntValue, dtype: int32
0    999
1     50
2     50
3     55
4     58

I would like the adjusted column to be adjusted to look like:

Name: IntValue, dtype: int32
0    
1     50
2     50
3     55
4     58

I am using:

rawDatabase[newFieldTrunc].replace(999, "")

But it's not changing the columns data at all.

Upvotes: 0

Views: 575

Answers (3)

Joe
Joe

Reputation: 12417

As alternative to the previous answers:

 rawDatabase[newFieldTrunc] = rawDatabase[newFieldTrunc].replace(999, "")

Upvotes: 0

iDrwish
iDrwish

Reputation: 3103

Because you are replacing over a copy of the Series, not the actual Series.

rawDatabase[newFieldTrunc].replace(999, "", inplace=True)

Or

rawDatabase[newFieldTrunc] = rawDatabase[newFieldTrunc].replace(999, "")

PS: It is advised to use Python native None or np.nan as a placeholder for blanks instead of a blank text "".

Upvotes: 1

sjw
sjw

Reputation: 6543

To alter the DataFrame inplace, you need to set the inplace argument to True:

rawDatabase[newFieldTrunc].replace(999, "", inplace=True)

Upvotes: 0

Related Questions