convert pandas dataframe column from hex string to int
The reverse operation (float to hex lossless conversion) would be:
df['Command0'].apply(float.hex)
You can use apply
as per @Andrew's solution, but lambda
isn't necessary and adds overhead. Instead, use apply
with a keyword argument:
res = df['Command0'].apply(int, base=16)
print(res)
0 456
1 195
Name: Command0, dtype: int64
With pd.read_csv
, you can use functools.partial
:
from functools import partial
df = pd.read_csv(open_csv, nrows=20, converters={'Command0': partial(int, base=16)})
You could use apply
.
df.Command0.apply(lambda x: int(x, 16))
>>>
0 456
1 195
Name: Command0, dtype: int64
And you can do this with pd.read_csv
call using the converters
parameter:
df = pd.read_csv("path.txt", converters={"Command0": lambda x: int(x, 16)})