Pandas – convert integer to bytestring and update single fields

Question:

Problem: I receive from a machine errorcodes as integer (every minute). This errorcode represents a bytestring and every bit stands for a specific error. For analytics I need this bits
inside pandas as separate columns. I’m struggling doing this.

def converttobinary(num, length=3):
    binary_string_list = list(format(num, '0{}b'.format(length)))
    return [int(digit) for digit in binary_string_list]


df = pd.DataFrame({'errorcode': [1,2,3],
                    'errorcodelist': ['', '', ''],
                    'errorcode01': ['', '', ''],
                    'errorcode02': ['', '', ''],
                    'errorcode03': ['', '', ''],})
df['errorcodelist'] = df.apply(lambda row : converttobinary(row['errorcode']), axis = 1)

print(df)

Result (by now):

errorcode errorcodelist errorcode01 errorcode02 errorcode03
1         [0, 0, 1]
2         [0, 1, 0]
3         [0, 1, 1]

Expected Result:

errorcode errorcodelist errorcode01 errorcode02 errorcode03
1         [0, 0, 1]         1           0           0
2         [0, 1, 0]         0           1           0
3         [0, 1, 1]         0           1           1

Has anyone an idea, how to get the expected result?

Asked By: fxopattern

||

Answers:

You can use another apply call to extract the element of the list:

df['errorcode01'] = df.apply(lambda row : row['errorcodelist'][2], axis = 1)
df['errorcode02'] = df.apply(lambda row : row['errorcodelist'][1], axis = 1)
df['errorcode03'] = df.apply(lambda row : row['errorcodelist'][0], axis = 1)

Or you could fill the other columns during dataframe creation:

from collections import defaultdict
errorcodes = [1,2,3]

# create dataframe
obj = defaultdict(list)
bit_length = 3
for code in errorcodes:
    obj["errorcode"].append(code)

    binary_list = converttobinary(code, length=bit_length)
    obj["errorcodelist"].append(binary_list)
    
    # add the bits to the corresponding list
    for i, bit in enumerate(reversed(binary_list)):
        obj[f"bit{i+1:02d}"].append(bit)

df = pd.DataFrame(obj)
Answered By: Runinho
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.