Categories
Python Answers

How to display a decimal in scientific notation with Python?

Sometimes, we want to display a decimal in scientific notation with Python.

In this article, we’ll look at how to display a decimal in scientific notation with Python.

How to display a decimal in scientific notation with Python?

To display a decimal in scientific notation with Python, we can use the %.2E placeholder.

For instance, we write

from decimal import Decimal

n = '%.2E' % Decimal('40800000000.00000000000000')

to format the decimal number to a string version of the number in scientific notation with the %.2E format placeholder.

Conclusion

To display a decimal in scientific notation with Python, we can use the %.2E placeholder.

Categories
Python Answers

How to do currency formatting in Python?

Sometimes, we want to do currency formatting in Python.

In this article, we’ll look at how to do currency formatting in Python.

How to do currency formatting in Python?

To do currency formatting in Python, we can use the locale module.

For instance, we write

import locale

locale.setlocale(locale.LC_ALL, '')
s = locale.currency(5676766.56, grouping=True)

to call setlocale to set the locale.

Then we call locale.currency with the currency value and the grouping argument to return a string with the number formatted as a currency.

We set grouping to True so that the digits are grouped with thousand separators.

Conclusion

To do currency formatting in Python, we can use the locale module.

Categories
Python Answers

How to save a pandas DataFrame table as a png with Python?

Sometimes, we want to save a pandas DataFrame table as a png with Python.

In this article, we’ll look at how to save a pandas DataFrame table as a png with Python.

How to save a pandas DataFrame table as a png with Python?

To save a pandas DataFrame table as a png with Python, we can use the data frame export method in the dataframe_image package.

To install it, we run

pip install dataframe_image

Then we write

import pandas as pd
import numpy as np
import dataframe_image as dfi

df = pd.DataFrame(np.random.randn(6, 6), columns=list('ABCDEF'))
df_styled = df.style.background_gradient()
dfi.export(df_styled, "mytable.png")

to create a 6 column data frame with some random numbers with

df = pd.DataFrame(np.random.randn(6, 6), columns=list('ABCDEF'))

Then we call df.style.background_gradient to style the data frame table with a background gradient.

Finally, we call dfi.export with the df_styled data frame and the file name to save the table image to.

Conclusion

To save a pandas DataFrame table as a png with Python, we can use the data frame export method in the dataframe_image package.

Categories
Python Answers

How to save and load multiple objects in pickle file with Python?

Sometimes, we want to save and load multiple objects in pickle file with Python.

In this article, we’ll look at how to save and load multiple objects in pickle file with Python.

How to save and load multiple objects in pickle file with Python?

To save and load multiple objects in pickle file with Python, we can call pickle.load to load all the objects that are pickled in the file.

For instance, we write

def loadall(filename):
    with open(filename, "rb") as f:
        while True:
            try:
                yield pickle.load(f)
            except EOFError:
                break

items = loadall(my_filename)

to create the loadall function that opens the filename file with open.

In the with block, we create a while loop that yields the object returned by picke.load until a EOFError is raised.

Once the error is raised, we read all the items in the pickled file.

Conclusion

To save and load multiple objects in pickle file with Python, we can call pickle.load to load all the objects that are pickled in the file.

Categories
Python Answers

How to print all instances of a class with Python?

Sometimes, we want to print all instances of a class with Python.

In this article, we’ll look at how to print all instances of a class with Python.

How to print all instances of a class with Python?

To print all instances of a class with Python, we can use the instances property.

For instance, w write

class A:
    instances = []
    def __init__(self):
        self.__class__.instances.append(self)

to create the A class with the instances attribute list.

Then we call self.__class__.instances.append to append self to the instances attribute when we create an A instance.

Then we can get the value of instances with

print('\n'.join(A.instances))

to print all instances of class A created.

Conclusion

To print all instances of a class with Python, we can use the instances property.