Python number with 1000 Separator
To get the number with ,
as a thousands separator as a whole number, use this format string:
number_formated = "{:,.0f}".format(number)
No need to nest two calls and cast the resulting string to int again.
ipython: print numbers with thousands separator
Using '
as thousands separator in input is quite problematic because Python uses '
to delimit strings, but you can use _
(PEP 515, Underscores in Numeric Literals):
Regarding output, this is slightly harder, but can be done using IPython extensions.
Put the following Python code in a new file at ~/.ipython/extensions/thousands_separator.py:
default_int_printer = None
def print_int(number, printer, cycle):
printer.text(f'{number:,}') # You can use `'{:,}'.format(number)` if you're using a Python version older than 3.6
def load_ipython_extension(ipython):
global default_int_printer
default_int_printer = ipython.display_formatter.formatters['text/plain'].for_type(int, print_int)
def unload_ipython_extension(ipython):
ipython.display_formatter.formatters['text/plain'].for_type(int, default_int_printer)
This code tells IPython to replace the default int
formatter with one that prints thousand separators when this extension is loaded, and restore the original when it is unloaded.
Edit: If you want a different separator, for instance '
, replace the f'{number:,}'
with f'{number:,}'.replace(',', "'")
.
You can load the extension using the magic command %load_ext thousands_separator
and unload it using %unload_ext thousands_separator
, but if you want it always, you can place it in the default profile.
Run the following code in the terminal:
ipython3 profile create
It will report that a file ~/.ipython/profile_default/ipython_config.py was created. Enter it, and search for the following string:
## A list of dotted module names of IPython extensions to load.
#c.InteractiveShellApp.extensions = []
Replace it with the following:
# A list of dotted module names of IPython extensions to load.
c.InteractiveShellApp.extensions = [
'thousands_separator'
]
This tells IPython to load this extension by default.
Done!
Edit: I saw that you want to a) use '
as separator, and b) do the same for floats:
Using different separator is quite easy: just str.replace()
:
def print_int(number, printer, cycle):
printer.text(f'{number:,}'.replace(',', "'"))
Doing the same for floats is also easy: just setup print_int
so it prints floats to. I also suggest to change the name to print_number
.
Final code:
default_int_printer = None
default_float_printer = None
def print_number(number, printer, cycle):
printer.text(f'{number:,}'.replace(',', "'"))
def load_ipython_extension(ipython):
global default_int_printer
global default_float_printer
default_int_printer = ipython.display_formatter.formatters['text/plain'].for_type(int, print_number)
default_float_printer = ipython.display_formatter.formatters['text/plain'].for_type(float, print_number)
def unload_ipython_extension(ipython):
ipython.display_formatter.formatters['text/plain'].for_type(int, default_int_printer)
ipython.display_formatter.formatters['text/plain'].for_type(float, default_float_printer)
Formatting numbers with localized thousands separators and decimal precision in python
Add a comma - the thousands separator - before the dot, and do a replace with the appropriate separator of your locale:
>>> my_sep = locale.localeconv()['thousands_sep']
>>> print("{:,.2f}".format(7654321.234567).replace(',', my_sep))
7 654 321.23
Convert numbers in thousands separator into float
The easiest way is to just remove the commas, then call float
.
>>> s = '123,456.78'
>>> float(s.replace(',',''))
123456.78
In your case, you'll want pandas.map
:
data[column] = data[column].map(lambda s: s.replace(',',''))
This will apply the function to every value in the column.
Related Topics
Getting S3 Objects' Last Modified Datetimes With Boto
Collect_List by Preserving Order Based on Another Variable
How to Delete a Character in an Item in a List (Python)
Opencv Typeerror: Expected Cv::Umat for Argument 'Src' - What Is This
Masking Horizontal and Vertical Lines With Open Cv
Add Excel File Attachment When Sending Python Email
How to Normalize a 2-Dimensional Numpy Array in Python Less Verbose
Exclude First Row When Importing Data from Excel into Python
Check If List Is Ascending or Descending (Using For)
Setting Matplotlib Colorbar Range
Loop Through Json Data in Python
Selecting Specific Rows of CSV Based on a Column'S Value in Python
Missing 1 Required Positional Argument - Issue
Package Only Binary Compiled .So Files of a Python Library Compiled With Cython
Beautifulsoup: Get the Contents of a Specific Table