I have a pandas dataframe called df10
.
cols10 = list(df10.columns)
I want to change the data type to double
where the column name starts with "m_"
for param in cols10:
if param.startswith("m_"):
df10[[param]] = df10[[param]].apply(pd.to_double)
However, I'm getting this error:
AttributeError: 'module' object has no attribute 'to_double'
the function to_double
doesn't exist in pandas. pandas datatypes are essentially numpy data types
i'm assuming you mean float64 by double
you can either let numpy decide the precision for you
for col in cols10:
if col.startswith('m_'):
df[col] = df[col].astype(float)
or specify the precision yourself
for col in cols10:
if col.startswith('m_'):
df[col] = df[col].astype(np.float64) # or np.float32 or np.float16
You can do this without loop,
reqd_cols = df10.columns[df10.columns.str.startswith('m_')]
df10[reqd_cols] = df10[reqd_cols].astype('float64')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With