我需要使用不同的函数来处理数值列和字符串列。我现在做的事真的很蠢:
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])
有没有比这更优雅的方法?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])
可以使用^{} 访问列的数据类型:
我知道这有点老生常谈,但对于熊猫19.02,你可以做到:
http://pandas.pydata.org/pandas-docs/version/0.19.2/generated/pandas.DataFrame.select_dtypes.html
在
pandas 0.20.2
中,您可以执行以下操作:所以你的代码变成:
相关问题 更多 >
编程相关推荐