資料處理 一些比賽中通用的函式

2021-10-06 18:56:49 字數 1493 閱讀 5046

這篇文章主要記載一些非具體化的函式操作,以及一些加速,壓縮等可用可不用的函式,具體到專案的資料處理函式詳情見其他文章。

def reduce_mem_usage(df, verbose=true):

numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']

start_mem = df.memory_usage().sum() / 1024 ** 2

for col in df.columns:

col_type = df[col].dtypes

if col_type in numerics:

c_min = df[col].min()

c_max = df[col].max()

if str(col_type)[:3] == 'int':

if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:

df[col] = df[col].astype(np.int8)

elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:

df[col] = df[col].astype(np.int16)

elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:

df[col] = df[col].astype(np.int32)

elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:

df[col] = df[col].astype(np.int64)

else:

if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:

df[col] = df[col].astype(np.float16)

elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:

df[col] = df[col].astype(np.float32)

else:

df[col] = df[col].astype(np.float64)

end_mem = df.memory_usage().sum() / 1024 ** 2

if verbose: print('mem. usage decreased to mb (% reduction)'.format(end_mem, 100 * (

start_mem - end_mem) / start_mem))

return df

資料處理的一些方法

1 保留小數點後兩位 四捨五入alert num.tofixed 2 2 保留小數點後兩位 把後面捨去math.floor 15.7784514000 100 100 輸出結果為 15.77 yuantocent yuan yuantocent 5.55 100 呼叫時需要除以100之後數值才是對應...

sql中的一些通用函式

1.sql中使用case,when,then select case type when 1then 正常 when 2then 密碼錯誤 else 不正常 end 狀態 from tbl user 或者select case when type 1then 正常 when type 1then 密...

總結一些通用的處理方法

判斷當前值是否為不為空的數字 param num static isnumber num else 為乙個數字增加千分位 param num static thousandsforamt num g,function 1 return res 為所有傳進來的值小數補零 param num param...