Converting alphanumeric values to numeric values in a Python pandas dataframe

128 views Asked by At
   Timestamp CAN ID  Byte DATA[0] DATA[1] DATA[2] DATA[3] DATA[4]  \
1693260  1.478200e+09   0000   8.0      00      00      00      00      00   
732574   1.478199e+09   0000   8.0      00      00      00      00      00   
602503   1.478199e+09   02c0   8.0      14      00      00      00      00   
65525    1.478198e+09   0430   8.0      00      00      00      00      00   
1659292  1.478200e+09   0440   8.0      ff      00      00      00      ff   
46083    1.478198e+09   0002   8.0      00      00      00      00      00   
386324   1.478199e+09   0329   8.0      0c      bc      7f      14      11   
625751   1.478199e+09   0130   8.0      11      80      00      ff      0e   
1647482  1.478200e+09   0002   8.0      00      00      00      00      00   
1403442  1.478200e+09   0000   8.0      00      00      00      00      00   

        DATA[5] DATA[6] DATA[7]      AttackType  
1693260      00      00      00      DoS Attack  
732574       00      00      00      DoS Attack  
602503       00      00      00  Normal Message  
65525        00      00      00  Normal Message  
1659292      d2      08      00  Normal Message  
46083        06      01      a2  Normal Message  
386324       20      00      14  Normal Message  
625751       80      0b      e7  Normal Message  
1647482      03      09      2b  Normal Message  
1403442      00      00      00      DoS Attack 

Most values in my dataframe are alphanumeric. How do i change the alphabeths in the alphanumeric values to numeric values?

I tried changing the alphanumeric values to nan values and dropping the rows but that didn't work because almost every row in the dataframe contained alphanumeric values. Please help

1

There are 1 answers

2
hbwales On

Your CAN ID and DATA[0] ... DATA[7] columns are very likely hexadecimal numbers, you can convert them to decimal with:

df = ... # get DataFrame

cols = ['DATA[0]', 'DATA[1]', 'DATA[2]', 'DATA[3]']  # list of columns to convert
df[cols] = df[cols].applymap(int, base=16)           # do conversion

(Note this does not match the interpretation of these columns in your comment, if you are certain on that interpretation then the solution will be very similar, just pass your own function into applymap )