6

I currently have a string of values which I retrieved after filtering through data from a csv file. ultimately I had to do some filtering of the data but I have the same numbers as a list, dataframe, or array. I just need to take the numbers in the string and convert them to hex and then take the first 8 numbers of the hex and convert that to dec for each element in the string. Lastly I also need to convert the last 8 of the same hex and then to dec as well for each value in the string.

I cannot provide a snippet because it is sensitive data, but here is an example.

I basically have something like this

>>> list_A

[52894036, 78893201, 45790373]

If I convert it to a dataframe and call df.dtypes, it says dtype: object and I can convert the values of Column A to bool, int, or string, but the dtype is always an object.

It does not matter whether it is a function, or just a simple loop. I have been trying many methods and am unable to attain the results I need. But ultimately the data is taken from different csv files and will never be the same values or list size.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
rsotommx
  • 77
  • 1
  • 2
  • 7
  • When you say list, do you mean like this: ['1234', '0', '5678'] – Patrick Maupin Jul 21 '15 at 02:03
  • So to elaborate and simplify the question, I currently decided to create an array of numbers. I am able to convert the values in the array to hex. But the Hex is a combination of two separate decimal numbers. So I have to take the 1st 6 char in hex and convert that to dec (excluding the 'ox' portion) and also take the last 2 char in the hex value and convert that to decimal individually as well. This is what I used to create my hex array. hex_array = [hex(x) for x in dec_array] – rsotommx Jul 21 '15 at 16:40

1 Answers1

8

Pandas is designed to work primarily with integers and floats, with no particular facilities for hexadecimal that I know of, but you can use apply to access standard python conversion functions like hex and int:

df=pd.DataFrame({ 'a':[52894036999, 78893201999, 45790373999] })
df['b'] = df['a'].apply( hex )
df['c'] = df['b'].apply( int, base=0 )

Results:

             a             b            c
0  52894036999   0xc50baf407  52894036999
1  78893201999  0x125e66ba4f  78893201999
2  45790373999   0xaa951a86f  45790373999

Note that this answer is for Python 3. For Python 2 you may need to strip off the trailing "L" in column "b" with str[:-1].

JohnE
  • 29,156
  • 8
  • 79
  • 109
  • So to elaborate and simplify the question, I currently decided to create an array of numbers. I am able to convert the values in the array to hex. But the Hex is a combination of two separate decimal numbers. So I have to take the 1st 6 char in hex and convert that to dec (excluding the 'ox' portion) and also take the last 2 char in the hex value and convert that to decimal individually as well. This is what I used to create my hex array. hex_array = [hex(x) for x in dec_array] – rsotommx Jul 21 '15 at 16:36
  • Thank you for your help, I found the errors in my code and your snippet works perfectly fine – rsotommx Jul 21 '15 at 18:13
  • Some more options here that can use via `apply`, as above: https://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python?rq=1 – JohnE Dec 10 '20 at 15:25