 # I need to change if a number in column is -after numbers

I need to change the column to negative if any number in column is 213-.Below if the table
AA BB CC DD
A ZZ AD 34- Change this to -34

Hey Sachin,
First off I assume you are working with a pandas DataFrame here, is that right?

Would you be able to explain a bit more about what you are struggling with, I’m not quite understanding your issue.

Are you dealing with an integer column here, and wanting to flip the sign based on some condition?

HI Lewis,
I am trying to pull some data from Pdf, using pandas, pdfplumber, glob and re.
After pulling the required data, mentioned table is my output. But for negative number its pulling as mentioned in table. i need to convert it to negative number.

See if it works:

import pandas as pd
import openpyxl
ws=wb.get_sheet_by_name(“Sheet1”)
i=1
st=1
nv=""
while i<7:

``````mvL=ws._get_cell(i, 1).value

mval=(mvL)
if mval[-1]=="-":
st=1
while st<len(mvL):
if mval[-(st+1)]=="0" or mval[-(st+1)]=="1" or mval[-(st+1)]=="2" or mval[-(st+1)]=="3" or mval[-(st+1)]=="4" or mval[-(st+1)]=="5" or mval[-(st+1)]=="6" or mval[-(st+1)]=="7" or mval[-(st+1)]=="8" or mval[-(st+1)]=="9":
nv=(mval[-(st+1)])+nv
st=st+1

print("-"+nv)
else:
print("NA")
i=i+1
st=1
nv=""``````

So to do this you need a couple of things. First off you can use the `.apply` method of a dataframe column to apply a function to each item in the column, and then the second thing you need is `.endswith` to see if a string ends with a `-`. The ones ending with `-` need the final character removing (with `[:-1]`) and then converting to an int and negating. The code would look something like:

``````def fix_negatives(val):
if val.endswith('-'):
return -1 * int(val[:-1])
return int(val)
df['DD'] = df['DD'].apply(fix_negatives)
``````

This might need changing slightly if your text ends up looking slightly different, but this should give you somewhere to start

I am getting error, as the number column is not in Int. I checked the data type it comes as Object. and also the numbers are in millions (2,345,789.78).
Below is the error.
invalid literal for int() with base 10: ‘8,792,234.53’

Ah okay so your numbers are floats instead of ints. Try replacing `int` with `float` in the above code, to cast the strings to floats

I tried, still getting error. Then I used [DD] = float(DD). Still not working.
ValueError: could not convert string to float: ‘1,646,309.84’

You’ll need to remove the commas from the string before doing float. You can do that with `val.replace(',', '') ` to replace all commas with an empty string