[Python] Assign pandas dataframe column dtypes

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I want to set the dtypes of multiple columns in pd.Dataframe (I have a file that I've had to manually parse into a list of lists, as the file was not amenable for pd.read_csv)

    import pandas as pd
    print pd.DataFrame([['a','1'],['b','2']],
    dtype={'x':'object','y':'int'},
    columns=['x','y'])


    I get

    ValueError: entry not a 2- or 3- tuple


    The only way I can set them is by looping through each column variable and recasting with astype.

    dtypes = {'x':'object','y':'int'}
    mydata = pd.DataFrame([['a','1'],['b','2']],
    columns=['x','y'])
    for c in mydata.columns:
    mydata[c] = mydata[c].astype(dtypes[c])
    print mydata['y'].dtype #=> int64


    Is there a better way?

    Continue reading...

Compartilhe esta Página