17

Is there a generic way to convert all float64 values in a pandas dataframe to float32 values? But not changing uint16 to float32? I don't know the signal names in advance but just want to have no float64.

Something like:

if float64, then convert to float32, else nothing?

The structure of the data is:

DF.dtypes

Counter               uint16
p_007                 float64
p_006                 float64
p_005                 float64
p_004                 float64

2 Answers 2

26
  1. If the dataframe (say df) wholly consists of float64 dtypes, you can do:
df = df.astype('float32')
  1. Only if some columns are float64, then you'd have to select those columns and change their 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')
Sign up to request clarification or add additional context in comments.

1 Comment

Why is select_dtypes() taking a while? does it scan through all rows...?
16

Try this:

df[df.select_dtypes(np.float64).columns] = df.select_dtypes(np.float64).astype(np.float32)

1 Comment

Perfect solution! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.