如何将 Pandas 中的所有 float64 列转换为 float32?

How to convert all float64 columns to float32 in Pandas?

是否有一种通用方法可以将 pandas 数据帧中的所有 float64 值转换为 float32 值?但不将 uint16 更改为 float32?我事先不知道信号名称,只是希望没有 float64。

类似于:

if float64, then convert to float32, else nothing?

数据结构为:

DF.dtypes

Counter               uint16
p_007                 float64
p_006                 float64
p_005                 float64
p_004                 float64

试试这个:

df[df.select_dtypes(np.float64).columns] = df.select_dtypes(np.float64).astype(np.float32)
  1. 如果数据框(比如df)完全由float64 dtypes组成,你可以这样做:
df = df.astype('float32')
  1. 仅当某些列是 float64 时,您才必须 select 这些列并更改它们的数据类型:
# Select columns with 'float64' dtype  
float64_cols = list(df.select_dtypes(include='int32'))

# The same code again calling the columns
df[float64_cols] = df[float64_cols].astype('float32')