Python How to convert float as hex to decimal

I was reading some data from a csv file with pandas. The data is incomplete and therefore contains many nan values. I want to add a column to data that converts hex values ​​to decimal values. Unfortunately, a column with hex values ​​is read as floats, not strings, because they just have those values. Sample data

   val
0 20.0
1  nan
2 20.0

      

The easy way to convert hex to decimal in python seems to be:, int('20.0',16)

which should give 32

.

However, since this is pandas, I cannot convert the values ​​to int, or at least I keep getting the error. My current code:

df['valdec'] = np.where(np.isnan(df['val']), 
                    df['val'], 
                    int(df['val'].astype(int).astype(str), 16))

      

Error with error:

ValueError: Cannot convert NA to integer

without a astype(int)

value "20.0"

that cannot be converted. Is there any other way to interpret the float value as a hex value and convert to decimal when working with a pandas dataframe?

+3


source to share


1 answer


You can mask the lines of interest and double click and call apply

:

In [126]:
df['valdec'] = df['val'].dropna().astype(int).astype(str).apply(lambda x: int(x, 16))
df

Out[126]:
    val  valdec
0  20.0    32.0
1   NaN     NaN
2  20.0    32.0

      

So first we call dropna

to delete NaN

, this allows us to use int

with .astype(int)

, then convert to str

by calling .astype(str)

.

Then we call apply

this to convert to hex and assign the result of all this to a new column



Note that there dtype

will be a new column float

, since presence NaN

enforces this, you cannot have a mixture int

and float

s

As @jasonharper pointed out, casting to int

here will lose any fractional so a higher precision method would use float.fromhex

:

In [128]:
df['valdec'] = df['val'].astype(str).dropna().apply(lambda x: float.fromhex(x))
df

Out[128]:
    val  valdec
0  20.0    32.0
1   NaN     NaN
2  20.0    32.0

      

+2


source







All Articles