You need to pass in a bytes object, rather than a str. The typical way to go from str (a unicode string in Python 3) to bytes is to use the .encode() method on the string and specify the encoding you wish to use.

my_bytes = my_string.encode('utf-8')
Answer from Amber on Stack Overflow
🌐
Packtpub
subscription.packtpub.com › book › data › 9781804611302 › 2 › ch02lvl1sec21 › performing-binary-encoding
Chapter 2: Encoding Categorical Variables | Python Feature Engineering Cookbook
For example, integer 1 can be represented as sequence 10, integer 2 as 01, integer 3 as 11, and integer 0 as 00. The digits in the two positions of the binary string become the columns, which are the encoded representations of the original variable:
Discussions

machine learning - Binary encoding and its interpretation in Python - Data Science Stack Exchange
Because that same problem would ... scientists depend on binary encoding. $\endgroup$ ... $\begingroup$ I already know how to do OHE. My concern was whether the computer will interpret it as Pave > Grvl. Thank you. $\endgroup$ ... Consider an example, where people prefer Paved ... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
June 13, 2019
machine learning - How to use Binary Encoding of Categorical Columns to predict labels in Python? - Stack Overflow
I have 2 files test.csv and train.csv. The attribute values are categorical and I am trying to convert them into numerical values. I am doing the following: import category_encoders as ce encoder =... More on stackoverflow.com
🌐 stackoverflow.com
which encoding system/machanism is used in python binary read?
https://docs.python.org/3/library/functions.html#open As mentioned in the Overview, Python distinguishes between binary and text I/O. Files opened in binary mode (including 'b' in the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is included in the mode argument), the contents of the file are returned as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given. In binary mode, you get raw bytes. You have to decode the bytes yourself. A byte is an integer between 0 and 255. More on reddit.com
🌐 r/pythonhelp
2
2
February 13, 2023
Error loading open gzip log file in python
The issue is that you moved the utf-8 decoding stage (the part that converts binary to text) to inside your loop, so after your file's opening as text. What you normally do is either open in binary and do the decoding manually with open(my_file, 'rb') fp: for line in fp: line = line.decode('utf-8').other_stuff or open the file in text mode, and specify how the 'opener' should decode it with open(my_file, 'r', encoding='utf-8') fp: for line in fp: # no more .decode() needed the same applies to gzip's open, also note that it's strongly advised to use the same with: block as it ensures the file is properly closed, even when an exception occurs: import gzip import sys with gzip.open(sys.argv[1], 'r', encoding='utf-8') as f: for lines in f: line = lines.strip().split('"') if line[0].startswith("#") is True: continue date = line[0].split('"') etc etc Small sidenote: maybe you already new this, but the 'coding' line in your script at the top is just an indication of the script's encoding, not the encoding Python will use to read files with for example gzip.open(). For that it still uses the current encoding of your environment. More on reddit.com
🌐 r/learnpython
7
2
November 5, 2018
🌐
Analytics Vidhya
analyticsvidhya.com › home › what are categorical data encoding methods | binary encoding
What are Categorical Data Encoding Methods | Binary Encoding
May 1, 2025 - Binary encoding efficiently converts categories into binary digits, making it suitable for high cardinality datasets. In contrast, one-hot encoding creates separate binary columns for each category. A practical binary encoding example in Python can be implemented using libraries like category_encoders, streamlining the process of managing large datasets effectively.
🌐
IT trip
en.ittrip.xyz › python
The Essential Guide to Encoding and Decoding Binary Files in Python | IT trip
November 4, 2023 - This example packs an integer, a 4-byte string, and a short into a bytes object. Sometimes, you may want to convert binary data to a hexadecimal string for display or other purposes. Python’s `binascii` module is perfect for such operations. import binascii hex_str = binascii.hexlify(binary_data) Encoding and decoding binary files in Python is a versatile skill that’s applicable in many programming scenarios, from data storage to network communication.
🌐
GeeksforGeeks
geeksforgeeks.org › python › working-with-binary-data-in-python
Working with Binary Data in Python - GeeksforGeeks
July 15, 2025 - ... Input : 10011011 Output : 1001 1011 ---- 9B (in hex) 1001 1011 ---- 155 (in decimal) 1001 1011 ---- 233 (in octal) This clearly shows a string of bits can be interpreted differently in different ways.
Top answer
1 of 2
2

Your categorical variable has two levels, so there is no actual difference between dummy-coding vs. simply entering the variable into the analysis. That is, to dummy code you would create one new variable with two values but your original variable is already one variable with two values. Dummy-coding is important for variables with more than two possible values. So, in this case the computer won't consider Pave > Grvl.

But if you have more than two variables then you should use dummy variables.

For your data, you can use pandas.get_dummies() or sklearn's one hot encoder to achieve your result.

2 of 2
0
  1. How to encode?

sklearn.preprocessing provides various classes for this purpose, LabelBinarizer is one of them.

  1. Wouldn't the computer interpret this as Pave > Grvl?

Consider an example, where people prefer Paved house in comparison to Graveled. Then their is a relationship between values and hence it should be treated as something like you have mentioned, other wise it should be independent values(refer next answer).

  1. How does the computer differentiate between binary and integer encoding?

As I mentioned above, if the categorical values have some relationship(as mentioned above), then in such a case it should be integer values(0,1,2 and so on), otherwise it should be binary. Binary representation will help us in presenting as an independent value to ML model (however it doesn't make much sense in this case as you just have 2 values). But consider an example where a feature have more than 2 categorical values. If they all are independent then it should be represented as binary value i.e in the form of OneHotEncoding(refer sklearn.preprocessing classes).

🌐
AskPython
askpython.com › python › examples › binary-to-utf8-conversion
How to Convert Binary Data to UTF-8 in Python - AskPython
April 10, 2025 - Computers ultimately store all data as binary – sequences of 1’s and 0’s. This low-level binary data representation is great for storage and transmission efficiency. But binary data in raw form is not human-readable. So we have various encoding schemes that convert binary data to text characters and vice-versa. Here are some examples of binary data you may encounter in Python:
Find elsewhere
🌐
Opensource.com
opensource.com › article › 20 › 5 › base94-python
Use this Python script to encode binary in Base94 | Opensource.com
From this initial encoding, you can convert the data to any other base, as it's usually done with normal small decimal numbers. Here's a sample script, which I keep updated at on my GitHub repository. #!/usr/bin/env python3 from sys import argv from math import ceil base = 42 abc = '''!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~''' def to_base(fn_src, fn_dst, base=base): out_data = [] # represent a file as a big decimal number with open(fn_src, 'rb') as f: in_data = int.from_bytes(f.read(), 'big') # convert a big decimal number to a baseN d, r
🌐
Python Module of the Week
pymotw.com › 2 › base64
base64 – Encode binary data into ASCII characters - Python Module of the Week
There are no carriage returns in the output produced by the library, so I have broken the encoded data up artificially to make it fit better on the page. $ python base64_b64encode.py 113 bytes before encoding Expect 1 padding bytes 152 bytes after encoding CgppbXBvcnQgYmFzZTY0CgojIExvYWQgdGhpcyBz b3VyY2UgZmlsZSBhbmQgc3RyaXAgdGhlIGhlYWRl ci4KaW5pdGlhbF9kYXRhID0gb3BlbihfX2ZpbGVf XywgJ3J0JykucmVhZCgpLnNwbGl0KCc=
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › encoding-categorical-data-in-sklearn
Encoding Categorical Data in Sklearn - GeeksforGeeks
September 17, 2025 - Python · from sklearn.preprocessing import LabelEncoder le = LabelEncoder() df['class_encoded'] = le.fit_transform(df['class']) print("Class labels mapping:", dict(zip(le.classes_, le.transform(le.classes_)))) print(df[['class', 'class_encoded']].head()) Label Encoding · Now we will use One-Hot encoding which creates separate binary columns for each category, ideal for nominal data with no natural order.
🌐
Coders Tool
coderstool.com › blog › what-is-binary-encoding
Binary Encoding Utilities, Tools, and Technical
In Python, binary encoding usually means converting strings to bytes with an encoding like UTF-8, or formatting numbers as base-2 strings. Example:
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › base85-with-python
Base85 with Python | Binary Encoding Techniques Across Programming Languages
Python’s built-in base64 module provides a straightforward way to encode binary data into Base85 format using the b85encode function. This is particularly useful for representing binary information in a text-based medium, such as embedding ...
🌐
The Bscotch Blog
blog.bscotch.net › post › learn-how-binary-encoding-works
Learn how binary encoding works
June 13, 2022 - In Python 2.X, 3/2=1 but 3.0/2=1.5, while in Python 3.X both are 1.5. ... Data encodings are inventions, and you can encode things however you want to.
🌐
O'Reilly
oreilly.com › library › view › python-in-a › 0596100469 › ch22s01.html
Encoding Binary Data as Text - Python in a Nutshell, 2nd Edition [Book]
July 14, 2006 - The Python standard library supplies modules that support the standard encodings known as Base 64, Quoted Printable, and UU. The base64 module supports the encoding specified in RFC 1521 as Base 64. The Base 64 encoding is a compact way to represent arbitrary binary data as text, without any attempt to produce human-readable results.
Author: Alex Martelli
Published: 2006
Pages: 734
🌐
Python
docs.python.org › 3 › library › binary.html
Binary Data Services — Python 3.14.7 documentation
Some libraries described under Text Processing Services also work with either ASCII-compatible binary formats (for example, re) or all binary data (for example, difflib).
🌐
Real Python
realpython.com › python-bytes
Bytes Objects: Handling Binary Data in Python – Real Python
March 5, 2025 - The resulting bit pattern corresponds to two bytes, 255 and 214 in decimal, which encode the value -42 in two’s complement. Later, you restore one of the binary words to its original form by using int.from_bytes() and specifying matching argument values. Try changing those parameters to see their effect on the output. Other techniques for dealing with signed bytes and words in Python include using bitmasks and bit shifting, the modulo operator (%), as well as various modules that ship with Python: These are only examples, so you don’t need to fully understand them right now.
🌐
Medium
medium.com › @jaberi.mohamedhabib › encoding-categorical-variables-methods-and-techniques-in-pandas-scikit-learn-and-using-dummy-216ae2d5128d
Encoding Categorical Variables: Methods and Techniques in Pandas, Scikit-learn, and Using Dummy Function | by JABERI Mohamed Habib | Medium
September 27, 2024 - from sklearn.preprocessing import OrdinalEncoder # Sample data df = pd.DataFrame({'Size': ['Small', 'Medium', 'Large', 'Small', 'Large']}) # Ordinal Encoding oe = OrdinalEncoder(categories=[['Small', 'Medium', 'Large']]) df['Size_OrdinalEncoded'] ...