Created
February 3, 2020 18:41
-
-
Save h1ros/5ac5dff131dc2653cce52fd2bb001b05 to your computer and use it in GitHub Desktop.
save the momery by reducing the data type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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