You could use replace to remove all commas:
"10,000.00".replace(",", "")
You could use replace to remove all commas:
"10,000.00".replace(",", "")
>>> import re
>>> re.sub("[^\d\.]", "", "$1,000,000.01")
'1000000.01'
Regular expression pattern says "anything that isn't a number or a decimal point". Anything matching that regex is replaced with "".
You may need to bear in mind that some European countries use . as the thousand seperator and , as the decimal mark.
So "one million euros and 5 cents" could be €1.000.000,05 or €1,000,000.05
Hey guys, I have a csv file with any number greater than 999 being listed as a string in the form “1,000”. including the quotes. I’m trying to get rid of these commas so I can turn them into an integer, however I’m unsure how to do it without touching the other commas used to seperate the values. Any suggestions? So far I have thought this out but it’s not quite right.
‘’’ import pandas as pd df = pd.read_csv(‘..., sep = “, “)
firstline = True
if firstline: firstline = False else: for line in df: if “,” in line[3]: #the column with the values line[3].replace(“,”, “ “)
‘’’
Sorry for the formatting I am on phone. Thanks for the help :)
you need to capture the digits into group (\d+),(\d+)
import re
items = ['Hello, world!', 'Warhammer 40,000', 'Codename 1,337']
for item in items:
item = re.sub(r'(\d+),(\d+)', r'\1\2', item)
print(item)
Results:
Hello, world!
Warhammer 40000
Codename 1337
Using @uingtea regex, but for pandas dataframe, you can do in this way:
import pandas as pd
import re
df = pd.DataFrame({'col':['Hello, world!', 'Warhammer 40,000', 'Codename 1,337']})
df['col'] = df['col'].apply(lambda x: re.sub(r'(\d+),(\d+)', r'\1\2', x))
I have a csv file with a "Prices" column. Right now entries look like 1,000 or 12,456. I could probably remove them in Excel and re-save but I want to know how I can transform the column to remove non-numeric characters so 'objects' like $1,299.99 will become 'float' 1299.99. Thanks
Pandas has a built in replace method for "object" columns.
df["column"] = df["column"].str.replace(",","").astype(float)
Alternatively check out the pandas.to_numeric() function- I think this should work.
df["column"] = pd.to_numeric(df["column"])
You can also pass arguments for error handling with the pd.to_numeric() function. See the pandas documentation on it.
First, make a function that can convert a single string element to a float:
valid = '1234567890.' #valid characters for a float
def sanitize(data):
return float(''.join(filter(lambda char: char in valid, data)))
Then use the apply method to apply that function to every entry in the column. Reassign to the same column if you want to overwrite your old data.
df['column'] = df['column'].apply(sanitize)
You can do this :
>>> string = '82,000,00'
>>> int(price_result.replace(',', ''))
8200000
Checkout https://docs.python.org/2/library/string.html or https://docs.python.org/3/library/string.html depending on the Python version you are using and use the "replace()" function:
int_price = int(price_result.replace(',',''))
This replaces all commas within the string and then casts it to an INT:
>>> price = "1,000,000"
>>> type(price)
<type 'str'>
>>> int_price = int(price.replace(',',''))
>>> type(int_price)
<type 'int'>
>>>
I'm assuming this data was originally in a csv file where data that contains commas is quoted ("105,424","102,629","104,307") and then you are splitting on comma:
>>> '"105,424","102,629","104,307"'.split(',')
['"105', '424"', '"102', '629"', '"104', '307"']
Rather you should let the csv module do the work as it will handle the double quotes:
import csv
with open('u:\\foobar.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
print [x.replace(',','') for x in row]
This prints: ['105424', '102629', '104307']
Does your data look something like:
"123", "123,456", "123,456,789"
If so then try this
input = '"123", "123,456", "123,456,789"'
import re
reg = re.compile('"(\d{1,3}(,\d{3})*)"')
stringValues = [wholematch.replace(',', '') for wholematch, _endmatch
in reg.findall(input)]
This regex should also work on thousands with decimal places as well.
re.compile('"(\d{1,3}(,\d{3})*(\.\d*)?)"')