Skip to content

Instantly share code, notes, and snippets.

@h1ros
Created February 3, 2020 18:41
Show Gist options
  • Save h1ros/5ac5dff131dc2653cce52fd2bb001b05 to your computer and use it in GitHub Desktop.
Save h1ros/5ac5dff131dc2653cce52fd2bb001b05 to your computer and use it in GitHub Desktop.
save the momery by reducing the data type
def change_datatype(df):
int_cols = list(df.select_dtypes(include=['int']).columns)
for col in int_cols:
if ((np.max(df[col]) <= 127) and(np.min(df[col] >= -128))):
df[col] = df[col].astype(np.int8)
elif ((np.max(df[col]) <= 32767) and(np.min(df[col] >= -32768))):
df[col] = df[col].astype(np.int16)
elif ((np.max(df[col]) <= 2147483647) and(np.min(df[col] >= -2147483648))):
df[col] = df[col].astype(np.int32)
else:
df[col] = df[col].astype(np.int64)
def change_datatype_float(df):
float_cols = list(df.select_dtypes(include=['float']).columns)
for col in float_cols:
df[col] = df[col].astype(np.float32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment