Reputation: 1039
I've got data coming from embedded hardware. The messages come in the form of a long hexadecimal, but this hexadecimal is really the hardware concatenating several distinct groups of data in 4-character blocks. To confuse the matter, the database is converting this long hex into a decimal at a layer below what I have access to.
My dataframe:
dict = {'A': ['foo', 'bar', 'baz'], 'B': ['1346', '9953', '5246']}
df = pd.DataFrame(dict)
I want to convert column B
into a hexidecimal called LongHex
, split out the last 4 characters of LongHex
to create ShortHex
, convert ShortHex
back into an integer called ShortDec
, and finally wrap the columns LongHex
and ShortHex
with =""
so that Excel won't convert some of the hex values to scientific notation.
Here is what I've tried so far:
df['LongHex'] = df['B'].apply(lambda x: hex)
df['ShortHex'] = df['LongHex'].str[-4:]
df['ShortDec'] = df['ShortHex'].apply(lambda x: int)
df['LongHex'] = df['LongHex'].apply(str)
df['ShortHex'] = df['ShortHex'].apply(str)
df['LongHex'] = df['LongHex'].apply(lambda x: '="' + x + '"')
df['ShortHex'] = df['ShortHex'].apply(lambda x: '="' + x + '"')
Eventually this dataframe is output to .csv. When I open the file, this is what I get:
foo, 1346, <built-in function hex>, nan, <type 'int'>
bar, 9953, <built-in function hex>, nan, <type 'int'>
baz, 5246, <built-in function hex>, nan, <type 'int'>
Upvotes: 0
Views: 2323
Reputation: 14226
Change this:
df['LongHex'] = df['B'].apply(lambda x: hex)
df['ShortDec'] = df['ShortHex'].apply(lambda x: int)
To this:
df['LongHex'] = df['B'].apply(lambda x: hex(x))
df['ShortDec'] = df['ShortHex'].apply(lambda x: int(x))
Also as a side note I see you convert them to strings later why not do it all in one go as such:
df['LongHex'] = df['B'].apply(lambda x: str(hex(x))[-4:])
Upvotes: 1