text_plain_output_1.png
from sklearn.preprocessing import LabelEncoder

import pandas as pd

train = pd.read_csv('../input/tabular-playground-series-mar-2022/train.csv', index_col='row_id', parse_dates=['time'])
test = pd.read_csv('../input/tabular-playground-series-mar-2022/test.csv', index_col='row_id', parse_dates=['time'])
sub = pd.read_csv('../input/tabular-playground-series-mar-2022/sample_submission.csv')

missing_values_train = train.isna().any().sum()
missing_values_test = test.isna().any().sum()

duplicates_train = train.duplicated().sum()
duplicates_test = test.duplicated().sum()

def add_road_feature(df):
    df['road'] = df['x'].astype(str) + df['y'].astype(str) + df['direction']
    return df.drop(['x', 'y', 'direction'], axis=1)
train = add_road_feature(train)
test = add_road_feature(test)
le = LabelEncoder()
train['road'] = le.fit_transform(train['road'])
test['road'] = le.transform(test['road'])

group = ['road', 'weekday', 'hour', 'minute']
congestion = train.groupby(group).congestion
def add_feature(feature, feature_name):
    feature = feature.rename(columns={'congestion': feature_name})
    return (train.merge(feature, on=group, how='left'), test.merge(feature, on=group, how='left'))
train, test = add_feature(pd.DataFrame(congestion.max().astype(int)).reset_index(), 'min')
train, test = add_feature(pd.DataFrame(congestion.max().astype(int)).reset_index(), 'max')
train, test = add_feature(pd.DataFrame(congestion.median().astype(int)).reset_index(), 'median')

train.drop(['month', 'day', 'weekday', 'hour', 'minute', 'time'], axis=1, inplace=True)
test.drop(['month', 'day', 'weekday', 'hour', 'minute', 'time'], axis=1, inplace=True)
train.head()