text_plain_output_1.png
from sklearn.preprocessing import OneHotEncoder

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

data = pd.read_csv('/kaggle/input/income/train.csv')
data.shape

all_columns = list(data.columns)
all_columns

data.isna().sum()

cat_columns = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'gender', 'native-country']

def event_rate_analysis(column, target):
    temp = (data.groupby(column)[target].sum() / data[target].sum()).to_frame().reset_index()
    temp['Volume'] = (data.groupby(column)[target].count() / data[target].count()).to_list()
    temp = temp.sort_values(target, ascending=False)

from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder(sparse=False)
data_ohe = ohe.fit_transform(data[['race', 'gender', 'relationship', 'occupation', 'workclass', 'native-country', 'marital-status', 'education']])

cat_columns = []
for cat in ohe.categories_:
    cat_columns.extend(cat)

cat_df = pd.DataFrame(data_ohe, columns=cat_columns)
cat_df.head()