extracting first digit of a float64 variable

I imported a .xlsb file into python (pycharm IDE) by using pd.read_excel() function; however, before importing the .xlsb file, I convert it into .xlsx format and then import it. Please note that I did not use .xlsb because I faced issues regarding the parsing of the date variable.

Now I need to extract the first digit of a float64 type variable and I run the following code –

app_invoice = app_invoice \
    .assign (FIRSTDIGIT = int(str(app_invoice['INVOICE_AMOUNT'][:1]))

But it does not extract the first digit. Then I just checked the issue by running the following code –


but the output of it looks like this. I do not why it is \n1 and so on

0         13611.34\n1         91000.00\n2           159.97\n3          1300.00\n4  

>Solution :

Try using Series.apply explicitly.

first_digits = invoice['INVOICE_AMOUNT'].apply(lambda x: str(x)[:1])

that gets you a regular Python list of the digits; you can do something similar to turn it into a pandas column instead.

Leave a Reply