1

I have a bytestream that would like to read into the Python. I would like to use Pandas to hold the data. For example:

bytestream = '000102030404'

First I need to split the bytestream into different rows, with fixed size (2 bytes in this example).

bytestreamArray = ['0001', '0203', '0404']

Then I would like to split the array into two different columns

Col1 Col2
00 01
02 03
04 04

I wonder if I could do all in Pandas? Or I need to split the row in Python first then process it in Pandas?

Thanks in advance

1 Answer 1

1

You could use regex to find the pattern:

r'(\d\d)(\d\d)'

The (\d\d) looks for two digits, and assigns them to a group. I included the search for two groups to make it easier for what you were trying to do. I then use the output from the findall() function to generate a dataframe.

Here is the code:

import re
import pandas as pd

bytestream = '000102030404'

pattern = re.compile(r'(\d\d)(\d\d)')
byte_rows = re.findall(pattern, bytestream)

df = pd.DataFrame(byte_rows, columns=["Col1", "Col2"])

The Dataframe (df) looks like this:
enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

The above sample is the similfied version of my bytestream. I am still figuring out how to create the regex of my bytestream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.