仅使用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)

最新更新