how to check the dtype of a column in python pandas

I need to use different functions to treat numeric columns and string columns. What I am doing now is really dumb:

    allc = list((agg.loc[:, (agg.dtypes==np.float64)|(agg.dtypes==np.int)]).columns)
    for y in allc:
        treat_numeric(agg[y])    

    allc = list((agg.loc[:, (agg.dtypes!=np.float64)&(agg.dtypes!=np.int)]).columns)
    for y in allc:
        treat_str(agg[y])

Is there a more elegant way to do this? E.g.

    for y in agg.columns:
        if(dtype(agg[y]) == 'string'):
              treat_str(agg[y])
        elif(dtype(agg[y]) != 'string'):
              treat_numeric(agg[y])

You can access the data-type of a column with dtype:

    for y in agg.columns:
        if(agg[y].dtype == np.float64 or agg[y].dtype == np.int64):
              treat_numeric(agg[y])
        else:
              treat_str(agg[y])

From: stackoverflow.com/q/22697773

Back to homepage or read more recommendations: