如何检查python pandas中列的dtype [英] how to check the dtype of a column in python pandas
本文介绍了如何检查python pandas中列的dtype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用不同的函数来处理数字列和字符串列.我现在正在做的真的很愚蠢:
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])
这篇关于如何检查python pandas中列的dtype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文