Al Pan
Al Pan

Reputation: 39

changing values' type in dataframe columns

    df = pd.read_csv('alimenti.csv', delimiter=';')
    df.apply(pd.to_numeric, errors='ignore')
    print(df.dtypes)

I want to change quite all columns' values in numbers but the apply function ('to numeric') does not work on the dataframe. Hereunder the output of the types printed. Apart from the 2 first columns (descrizione, famiglia), all the remaining values should be float (or eventually integers).

    descrizione          object
    famiglia             object
    parte edibile         int64
    acqua                object
    proteine             object
    lipidi               object
    carboidrati          object
    amido                object
    zuccheri solubili    object
    fibra alimentare     object
    energia kcal          int64
    sodio                 int64
    potassio              int64
    ferro                object
    calcio                int64
    fosforo               int64
    tiamina              object
    riboflavina          object
    niacina              object
    vitamina a           object
    vitamina c            int64
    vitamina e           object
    quantita              int64
    dtype: object

Upvotes: 0

Views: 46

Answers (1)

Kastakin
Kastakin

Reputation: 129

As the pandas docs state you can change a column dtype with:

df = df.astype({'col1': 'desired_dtype','col2': 'desired_dtype'})

With col1 and col2 being the column name of the columns you want to change type.

Side note, I had a similar issue lately working with a dataset coming from old .xls files. The issues were missing data and corrupted values that made pd.read_excel retrive the column as dtype = object for the entire column instead of the desired float.

Upvotes: 1

Related Questions