Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert pandas dataframe column from hex string to int

I have a very large dataframe that I would like to avoid iterating through every single row and want to convert the entire column from hex string to int. It doesn't process the string correctly with astype but has no problems with a single entry. Is there a way to tell astype the datatype is base 16?

IN:
import pandas as pd
df = pd.DataFrame(['1C8','0C3'], columns=['Command0'])
df['Command0'].astype(int)
OUT:
ValueError: invalid literal for int() with base10: '1C8'

This works but want to avoid row iteration.

for index, row in df.iterrows():
    print(row['Command0'])

I'm reading this in from a CSV pd.read_csv(open_csv, nrows=20) so if there is a way to read it in and explicitly tell it what the format is then that would be even better!

like image 350
kaminsknator Avatar asked Jun 21 '16 22:06

kaminsknator


People also ask

How can I convert a hex string to an integer value?

To convert a hexadecimal string to a numberUse the ToInt32(String, Int32) method to convert the number expressed in base-16 to an integer. The first argument of the ToInt32(String, Int32) method is the string to convert. The second argument describes what base the number is expressed in; hexadecimal is base 16.

How do you convert a DataFrame data type?

The best way to convert one or more columns of a DataFrame to numeric values is to use pandas. to_numeric() . This function will try to change non-numeric objects (such as strings) into integers or floating-point numbers as appropriate.

How do you convert a DataFrame to an int?

Convert Column to int (Integer)Use pandas DataFrame. astype() function to convert column to int (integer), you can apply this on a specific column or on an entire DataFrame. To cast the data type to 64-bit signed integer, you can use numpy. int64 , numpy.

How do you find the hex value in Python?

hex() function in Python. hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.

How to convert string to integer in pandas Dataframe?

How to Convert String to Integer in Pandas DataFrame. In this guide, I’ll show you two methods to convert a string into an integer in pandas DataFrame: (1) The astype(int) method: df['DataFrame Column'] = df['DataFrame Column'].astype(int) (2) The to_numeric method: df['DataFrame Column'] = pd.to_numeric(df['DataFrame Column'])

How do I convert a column in a Dataframe to an int?

How to Convert Pandas DataFrame Columns to int You can use the following syntax to convert a column in a pandas DataFrame to an integer type: df ['col1'] = df ['col1'].astype(int) The following examples show how to use this syntax in practice.

How to create a Dataframe from a row in pandas?

Rows represents the records/ tuples and columns refers to the attributes. We can create the DataFrame by using pandas.DataFrame () method. We can also create a DataFrame using dictionary by skipping columns and indices.

How do I convert a string to an int in Python?

Convert multiple string column to int In this example, we are converting multiple columns that have a numeric string to int by using the astype (int) method of the Pandas library. We are using a Python dictionary to change multiple columns datatype Where keys specify the column and values specify a new datatype.


3 Answers

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)})
like image 137
jpp Avatar answered Oct 05 '22 13:10

jpp


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)})
like image 14
andrew Avatar answered Oct 05 '22 13:10

andrew


The reverse operation (float to hex lossless conversion) would be:

df['Command0'].apply(float.hex)

like image 1
mirekphd Avatar answered Oct 05 '22 11:10

mirekphd