我有这种方法,我只需要将其应用于'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':
答案 0 :(得分:2)
pandas
方法将首先使用columns
提取数字select_dtypes
subdf= df.select_dtypes(include='float32')
subdf=subdf.apply(preprocess,axis=1)
df[list(subdf)]=subdf
答案 1 :(得分:1)
您可以创建一系列float32类型的列,如下所示:
cols = dataframe.columns[dataframe.dtypes == 'float32']
然后将它们传递给您的函数:
dataframe[cols].apply(preprocess)