Python parse comma-separated number into int
>>> a = '1,000,000'
>>> int(a.replace(',', ''))
1000000
>>>
convert string numbers separated by comma to integers or floats in python
You want to split the string
total = 0
for i in s.split(','):
i = float(i) #using float because you don't only have integers
total += i
how to convert comma seperated values to integer in pandas
Here is a way, go through float type first:
df['no'].str.replace(',','').astype(float).astype(int)
Output:
0 1234450445
1 1234450446
2 1234450447
Name: no, dtype: int64
Or slice '.00' off then end of all rows:
df['no'].str.strip('.00').str.replace(',','').astype(int)
How can I convert a string with dot and comma into a float in Python
Just remove the ,
with replace()
:
float("123,456.908".replace(',',''))
How to convert a string to a number if it has commas in it as thousands separators?
import locale
locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' )
locale.atoi('1,000,000')
# 1000000
locale.atof('1,000,000.53')
# 1000000.53
How to convert comma separated numbers from a dataframe to to numbers and get the avg value
You can simply define a function that unpack those values and then get the mean of those.
def get_mean(x):
#split into list of strings
splited = x.split(',')
#Transform into numbers
y = [float(n) for n in splited]
return sum(y)/len(y)
#Apply on desired column
df['col'] = df['col'].apply(get_mean)
How to convert a string of space- and comma- separated numbers into a list of int?
Split on commas, then map to integers:
map(int, example_string.split(','))
Or use a list comprehension:
[int(s) for s in example_string.split(',')]
The latter works better if you want a list result, or you can wrap the map()
call in list()
.
This works because int()
tolerates whitespace:
>>> example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
>>> list(map(int, example_string.split(','))) # Python 3, in Python 2 the list() call is redundant
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
>>> [int(s) for s in example_string.split(',')]
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
Splitting on just a comma also is more tolerant of variable input; it doesn't matter if 0, 1 or 10 spaces are used between values.
Related Topics
Multiprocessing - Pipe VS Queue
How to Reduce the Image File Size Using Pil
Attributeerror: 'Client' Object Has No Attribute 'Send_Message' (Discord Bot)
Error When Loading Cookies into a Python Request Session
Group Dataframe and Get Sum and Count
Keep Same Dummy Variable in Training and Testing Data
How Does My Input Not Equal the Answer
Selenium: Get Coordinates or Dimensions of Element with Python
Python Regular Expression Pattern * Is Not Working as Expected
In Tensorflow, Differencebetween Session.Run() and Tensor.Eval()
How to Clone a Python Generator Object
Beautiful Soup 4 Find_All Don't Find Links That Beautiful Soup 3 Finds
Sample Each Group After Pandas Groupby
Cleanest Way to Hide Every Nth Tick Label in Matplotlib Colorbar