Python Forum
Replace only '-' values with NaN, don't remove minus sign for negative numbers.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace only '-' values with NaN, don't remove minus sign for negative numbers.
#1
I have '-' and some negative numbers '-2.5' in a column of data. How to replace only column values having only '-' with NaN, leaving negative numbers unchanged. Tried below didn't work.
Data[col]=Data[col].replace(to_replace='^-$', value='NaN')
Reply
#2
The simplest option is probably:
if Data[col] == '-':
    Data[col] = 'NaN'
Reply
#3
I think you need regex = True in your replace call for that to work.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  replace or remove text from many text files Curbie 21 3,246 Jul-07-2025, 02:05 PM
Last Post: Curbie
  Replace values in Yaml file with value in dictionary PelleH 1 3,821 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  remove duplicates from dicts with list values wardancer84 27 10,970 May-27-2024, 04:54 PM
Last Post: wardancer84
  negative memory usage akbarza 1 1,926 Apr-27-2024, 08:43 AM
Last Post: Gribouillis
  cmath.rect accepts a negative modulus JMB 2 7,241 Jan-17-2024, 08:00 PM
Last Post: JMB
  Does @ at sign used for tother than decorators? ggpython000 1 1,564 Oct-12-2023, 09:08 AM
Last Post: buran
  Remove values for weekend in a panda series JaneTan 0 1,909 Dec-12-2022, 01:50 AM
Last Post: JaneTan
  Remove numbers from a list menator01 4 5,560 Nov-13-2022, 01:27 AM
Last Post: menator01
  Getting error when running "MINUS" between 2 databases marlonbown 4 3,065 Nov-10-2022, 05:49 AM
Last Post: deanhystad
  Find and Replace numbers in String giddyhead 2 4,945 Jul-17-2022, 06:22 PM
Last Post: giddyhead

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020