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

Changing pandas strings to integer or float using `try: except:`

I have a dataframe full of strings that I would rather have as integers, or where that is not possible, floats.

I have tried to achieve this using try: except:.

The string "30" becomes an integer, but the string "40.0" becomes a float. I would like a column containing only whole-numbers as strings ("35.0", "41.0", "55.0") to become integers.

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

Here is a version of what I have attempted:

import pandas as pd

data = {
        'Name':['Tom', 'Dick', 'Harry'],
        'Age':['20', '21', '19'],
        'Height':['1.73', '1.80', '1.59'],
        'Score':['72.0', '69.0', '68.0']
        }
df = pd.DataFrame(data)

# Make list for looping through columns
cols = df.columns

# Check data types before manipulation
print(df.dtypes)

# Change to integer, or where not possible, float
for col in cols:
    try:
        df[col] = df[col].astype('int')
        continue
    except ValueError:
        pass
    try:
        df[col] = df[col].astype('float')
    except ValueError:
        pass

# Check if manipulation was successful
print(df.dtypes)

>Solution :

As Michael already commented, you can use pd.to_numeric with errors ignore.

for col in cols:
    df[col] = pd.to_numeric(df[col], errors='ignore')

No need for try excepts then.

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