Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

str(app_invoice['INVOICE_AMOUNT'])

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading