Categories
Python Answers

How to do frequency counts for unique values in an array with Python NumPy?

Sometimes, we want to do frequency counts for unique values in an array with Python NumPy.

In this article, we’ll look at how to do frequency counts for unique values in an array with Python NumPy.

How to do frequency counts for unique values in an array with Python NumPy?

To do frequency counts for unique values in an array with Python NumPy, we can use the unique method.

For instance, we write:

import numpy as np

x = np.array([1, 1, 1, 2, 2, 2, 5, 25, 1, 1])
unique, counts = np.unique(x, return_counts=True)

print(np.asarray((unique, counts)).T)

to create an array with np.array.

Then we call np.unique on array x and set return_counts to True to return the count of each item in the x array.

Finally, we call np.asarray with unique and counts in an tuple and get the T property to get the items and their counts in a nested list.

Therefore, we see:

[[ 1  5]
 [ 2  3]
 [ 5  1]
 [25  1]]

printed.

Conclusion

To do frequency counts for unique values in an array with Python NumPy, we can use the unique method.

Categories
Python Answers

How to convert string in base64 to image and save on file system with Python?

Sometimes, we want to convert string in base64 to image and save on file system with Python.

In this article, we’ll look at how to convert string in base64 to image and save on file system with Python.

How to convert string in base64 to image and save on file system with Python?

To convert string in base64 to image and save on file system with Python, we can use the base64.decodebytes method.

For instance, we write:

img_data = b'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='

import base64
with open("img.png", "wb") as fh:
    fh.write(base64.decodebytes(img_data))

We have a byte string with the base64 image data assigned to img_data.

Then we open the img.png file with open.

We open it with write permission by passing in 'wb'.

Then we call fh.write with base64.decodebytes(img_data)) to write the decoded base64 byte string as the content of the img.png to save the image.

As a result, img.png should have a red cross as its content.

Conclusion

To convert string in base64 to image and save on file system with Python, we can use the base64.decodebytes method.

Categories
Python Answers

How to convert integer to binary in Python?

Sometimes, we want to convert integer to binary in Python.

In this article, we’ll look at how to convert integer to binary in Python.

How to convert integer to binary in Python?

To convert integer to binary in Python, we can use the string’s format method.

For instance, we write:

b = '{0:08b}'.format(6)
print(b)

We use the {0:08b} format placeholder to convert the integer 6 to a binary number with 8 digits in the string.

Therefore, b is '00000110'.

Conclusion

To convert integer to binary in Python, we can use the string’s format method.

Categories
Python Answers

How to remove duplicate characters from a string with Python?

Sometimes, we want to remove duplicate characters from a string with Python.

In this article, we’ll look at how to remove duplicate characters from a string with Python.

How to remove duplicate characters from a string with Python?

To remove duplicate characters from a string with Python, we can use string’s join and the dict.fromkeys methods.

For instance, we write:

foo = "mppmt"
result = "".join(dict.fromkeys(foo))
print(result)

We use dict.fromkeys(foo) to get the characters in the string as keys to remove the duplicate characters and keep the characters in the same order.

Then we call join with the returned dictionary to join the characters together.

Therefore, result is 'mpt'.

Conclusion

To remove duplicate characters from a string with Python, we can use string’s join and the dict.fromkeys methods.

Categories
Python Answers

How to do time zone conversion with Python?

Sometimes, we want to do time zone conversion with Python.

In this article, we’ll look at how to do time zone conversion with Python.

How to do time zone conversion with Python?

To do time zone conversion with Python, we can use the pytz module.

For instance, we write:

from datetime import datetime
from pytz import timezone

fmt = "%Y-%m-%d %H:%M:%S %Z%z"
timezonelist = ['UTC', 'US/Pacific', 'Europe/Berlin']
for zone in timezonelist:
    now_time = datetime.now(timezone(zone))
    print(now_time.strftime(fmt))

We loop through timezonelist to print the current date and time in different time zones.

In the loop, we get the current date and time in the given zone with:

datetime.now(timezone(zone))

Then we call strftime with the fmt format string to return the date and time string from the date time object.

%Y is the year.

%m is the month.

%d is the day.

%H is the hour.

%M is the minute.

And %S is the seconds.

As a result, we get something like:

2021-10-31 20:19:08 UTC+0000
2021-10-31 13:19:08 PDT-0700
2021-10-31 21:19:08 CET+0100

printed.

Conclusion

To do time zone conversion with Python, we can use the pytz module.