相关疑难解决方法(0)

如何检查python pandas中列的dtype

我需要使用不同的函数来处理数字列和字符串列.我现在在做什么真是愚蠢:

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])    
Run Code Online (Sandbox Code Playgroud)

有没有更优雅的方式来做到这一点?例如

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

python pandas

112
推荐指数
6
解决办法
23万
查看次数

标签 统计

pandas ×1

python ×1