Convert pandas column dataframe from hex string to int

I have a very large data framework that I would like to avoid iterating through each row and want to convert the entire column from a hexadecimal string to int. It does not handle the string correctly with astype, but has no problems with a single entry. Is there any way to tell astype that the data type 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 you want to avoid string iteration.

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

I am reading this from CSV pd.read_csv(open_csv, nrows=20) , so if there is a way to read it and I will explicitly say what format it will be then it will be even better!

+5
source share
1 answer

You can use apply .

 df.Command0.apply(lambda x: int(x, 16)) >>> 0 456 1 195 Name: Command0, dtype: int64 

And you can do this by calling pd.read_csv using the converters parameter:

 df = pd.read_csv("path.txt", converters={"Command0": lambda x: int(x, 16)}) 
+6
source

All Articles