繁体   English   中英

仅使用Float32 dtype过滤熊猫数据框

[英]Filter Pandas Dataframe only with Float32 dtype

我有这种方法,我只需要适用于'float32'列,而不是所有列。

def preprocess(self, dataframe):
    if self._means is None: 
      self._means = np.mean(dataframe, axis=0)

    if self._stds is None:
      self._stds = np.std(dataframe, axis=0)
      if not self._stds.all():
        raise ValueError('At least one column has std deviation of 0.')

    return (dataframe - self._means) / self._stds

我收集这样的类型,但是正在寻找Pythonic的方法:

dtypes = list(zip(dataframe.dtypes.index, map(str, dataframe.dtypes)))
# Normalize numeric columns.
 for column, dtype in dtypes:
    if dtype == 'float32':

pandas方式将是首先使用select_dtypes提取数字columns

subdf= df.select_dtypes(include='float32') 
subdf=subdf.apply(preprocess,axis=1)
df[list(subdf)]=subdf 

您可以创建一系列float32类型的列,如下所示:

cols = dataframe.columns[dataframe.dtypes == 'float32']

然后将它们传递给您的函数:

dataframe[cols].apply(preprocess)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM