0

I exported a Hex file but the leading zeros are gone. How can I make them come back

I have this originally this:

   A    B   C
0  89   02  FC
1  89   02  FC
2  89   02  FC

When exported it became like this:

   A    B   C
0  89   2   FC
1  89   2   FC
2  89   2   FC

I tried this:

df['B'] = df['B'].apply(lambda x: '{0:0>3}'.format(x))

But I am getting this:

 A       C      C
0 89    02.0    FC
1 89    02.0    FC
2 89    02.0    FC

How can I export it and let it stay as Hex or ad leading zeros without been a float? (Leading zeros in position)?

falo8056
  • 85
  • 13
  • Use `df['B'] = df['B'].astype(str).apply(lambda x: '{0:0>2}'.format(x))` – jezrael Mar 02 '20 at 08:27
  • Or `df['B'] = df['B'].astype(str).str.zfill(2)` – jezrael Mar 02 '20 at 08:27
  • @jezrael Thanks for your fast reply. Nope. I tried that also. In both cases, I get 2.0 instead of 02. – falo8056 Mar 02 '20 at 08:37
  • 1
    i think there are floats, try `df['B'] = df['B'].astype(int).astype(str).apply(lambda x: '{0:0>2}'.format(x))` if not working because missing values `df['B'] = df['B'].astype('Int64').astype(str).apply(lambda x: '{0:0>2}'.format(x))` – jezrael Mar 02 '20 at 08:45
  • 1
    Exactly. @jezrael Actually, you are right, I search a bit more and found the floats problem. I solved differently but you answer is right as well and saves some lines of code. Thanks :) – falo8056 Mar 02 '20 at 08:48

0 Answers0