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
  cmath.rect accepts a negative modulus JMB 2 338 Jan-17-2024, 08:00 PM
Last Post: JMB
  Does @ at sign used for tother than decorators? ggpython000 1 545 Oct-12-2023, 09:08 AM
Last Post: buran
  Remove values for weekend in a panda series JaneTan 0 677 Dec-12-2022, 01:50 AM
Last Post: JaneTan
  Remove numbers from a list menator01 4 1,337 Nov-13-2022, 01:27 AM
Last Post: menator01
  Getting error when running "MINUS" between 2 databases marlonbown 4 1,270 Nov-10-2022, 05:49 AM
Last Post: deanhystad
  Find and Replace numbers in String giddyhead 2 1,243 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  How to do bar graph with positive and negative values different colors? Mark17 1 5,152 Jun-10-2022, 07:38 PM
Last Post: Mark17
  is there any tool to convert negative base to int? Skaperen 7 2,413 May-27-2022, 07:30 AM
Last Post: Gribouillis
  Need to sign JWT token with JWK key stucoder 1 1,693 Feb-21-2022, 09:04 AM
Last Post: stucoder
  Remove if similar values available based on two columns klllmmm 1 1,365 Feb-20-2022, 06:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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