Categories
Python Answers

How to insert list into a cell with Python Pandas?

Sometimes, we want to insert list into a cell with Python Pandas.

In this article, we’ll look at how to insert list into a cell with Python Pandas.

How to insert list into a cell with Python Pandas?

To insert list into a cell with Python Pandas, we can use at.

For instance, we write

df = pd.DataFrame(data={'A': [1, 2, 3], 'B': ['x', 'y', 'z']})
df.at[1, 'B'] = ['m', 'n']

to create the df data frame with the DataFrame class called with a dict.

Then we use df.at[1, 'B'] to get the value at column B with index and assign ['m', 'n'] to it.

Conclusion

To insert list into a cell with Python Pandas, we can use at.

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.