0

There are multiple columns in the df, out of which only selected columns has to be converted from hexa decimal to decimal

Selected column names are stored in a list A = ["Type 2", "Type 4"]

{'Type 1': {0: 1, 1: 3, 2: 5, 3: 7, 4: 9, 5: 11, 6: 13, 7: 15, 8: 17},
 'Type 2': {0: 'AA',
  1: 'BB',
  2: 'CC',
  3: '55',
  4: '88',
  5: '96',
  6: 'FF',
  7: 'FFFFFF',
  8: 'FEEE'},
 'Type 3': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0},
 'Type 4': {0: '23',
  1: 'fefe',
  2: 'abcd',
  3: 'dddd',
  4: 'dad',
  5: 'cfe',
  6: 'cf42',
  7: '321',
  8: '0'},
 'Type 5': {0: -120,
  1: -120,
  2: -120,
  3: -120,
  4: -120,
  5: -120,
  6: -120,
  7: -120,
  8: -120}}
Alice
  • 63
  • 4
  • You're asking [how to convert from hex to decimal](https://stackoverflow.com/questions/9210525/how-do-i-convert-hex-to-decimal-in-python), and then you just want to use the pandas apply function. Note that it's easier to get an answer if you provide the test data: there's an unnecessary hurdle in your question where we have to convert from PNG to python. You should supply the data as a dictionary (`df.to_dict()`) or in some other form that can easily be converted to a dataframe. – Kraigolas Mar 14 '21 at 19:23

2 Answers2

0

Say, you have the string "AA" in hex.

You can convert hex to decimal like this:

str(int("AA", 16))

Similarly, for a dataframe column that has hexadecimal values, you can use a lambda function.

df['Type2'] = df['Type2'].apply(lambda x: str(int(str(x), 16)))

Assuming, df is the name of the imported dataframe.

  • It works, but is there a way to ignore NaN and floating values or invalid string if present in the particular column? – Alice Mar 17 '21 at 08:20
  • For nans, you could try: df['column_name'] = df['column_name'].apply(lambda x: str(int(str(x), 16)) if(np.all(pd.notnull(x))) else x) – Ujjayanta Bhaumik Mar 18 '21 at 02:40
  • For me, I had wanted octal -> decimal, but values were saved as float. Above didn't work me, I used the similar: .apply(lambda x: int(str(int(x)), 8)) in case it helps anyone else. – Civil Dec 09 '21 at 04:57
0

You can use pandas.DataFrame.applymap to cast element-wise:

>>> df[["Type 2", "Type 4"]].applymap(lambda n: int(n, 16))
     Type 2  Type 4
0       170      35
1       187   65278
2       204   43981
3        85   56797
4       136    3501
5       150    3326
6       255   53058
7  16777215     801
8     65262       0
Pablo C
  • 4,661
  • 2
  • 8
  • 24