Home > front end >  Pandas - convert integer to bytestring and update single fields
Pandas - convert integer to bytestring and update single fields

Time:11-19

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?

CodePudding user response:

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)
  • Related