아미(아름다운미소)

boolean 예외처리 본문

랭귀지/pandas

boolean 예외처리

유키공 2024. 12. 18. 15:53

 

import pandas as pd

def fn_df(dict_df_types, df) -> pd.DataFrame:
    list_int = [k_ for (k_, v_) in dict_df_types.items() if (v_ != 'string') and (v_ != 'boolean') and (k_ in df.columns.to_list())]
    list_str = [k_ for (k_, v_) in dict_df_types.items() if (v_ == 'string') and (k_ in df.columns.to_list())]
    list_bool = [k_ for (k_, v_) in dict_df_types.items() if (v_ == 'boolean') and (k_ in df.columns.to_list())]
    
    df[list_str] = df[list_str].fillna('').astype('string')
    df[list_int] = df[list_int].assign(**{col: pd.to_numeric(df[col], errors='coerce').fillna(0).astype('int32') for col in list_int})
    df[list_bool] = df[list_bool].assign(**{
    	col: df[col].astype(str).str.lower()
        			.replace({'true': True, 'false': False})
                    .map({True: True, False: False, 'true': True, 'false': False})
                    .fillna(False)
                    .astype('boolean')
    	for col in list_bool})
        
    return df

dict_df_types = {
	'column1': 'int32',
    'column2': 'string',
    'column3': 'float',
    'column5': 'boolean',
}

data = {
	'column1': ['1', '2', 3, 'invalid', 4, 1],
    'column3': ['4.0', 5.5, 'invalid', 7.1, 5, 1],
    'column5': [True, '5.5', 'invalid', 'TrUE', 6, 'FaLsE'],
}

df = pd.DataFrame(data)

fn_process_dataframe = fn_df(dict_df_types, df)
print(fn_process_dataframe)
Comments