Mar*_*arK 13 python type-conversion pandas dtype
有没有一种通用方法可以将 pandas 数据框中的所有 float64 值转换为 float32 值?但不将 uint16 更改为 float32?我事先不知道信号名称,但只想没有 float64。
就像是:
if float64, then convert to float32, else nothing?
Run Code Online (Sandbox Code Playgroud)
数据的结构是:
DF.dtypes
Counter uint16
p_007 float64
p_006 float64
p_005 float64
p_004 float64
Run Code Online (Sandbox Code Playgroud)
小智 21
df)完全由float64数据类型组成,您可以执行以下操作:df = df.astype('float32')
Run Code Online (Sandbox Code Playgroud)
float64,您才必须选择这些列并更改它们的 dtype:# Select columns with 'float64' dtype
float64_cols = list(df.select_dtypes(include='float64'))
# The same code again calling the columns
df[float64_cols] = df[float64_cols].astype('float32')
Run Code Online (Sandbox Code Playgroud)
U10*_*ard 12
尝试这个:
df[df.select_dtypes(np.float64).columns] = df.select_dtypes(np.float64).astype(np.float32)
Run Code Online (Sandbox Code Playgroud)