text_plain_output_1.png
from collections import Counter

import numpy as np # linear algebra

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

hotel = pd.read_csv('/kaggle/input/hotel-booking-demand/hotel_bookings.csv')

hotel.shape

hotel.head().T

hotel_num = hotel.dtypes[hotel.dtypes != 'object']
hotel_num = hotel_num.index.to_list()
Date_Drop = {'is_canceled', 'company'}
hotel_num = [ele for ele in hotel_num if ele not in Date_Drop]
hotel_num
hot_num = hotel[hotel_num].copy()

from collections import Counter
def detect_outliers(df, features):
    """
    Takes a dataframe df of features and returns a list of the indices
    corresponding to the observations containing more than n outliers according
    to the Tukey method.
    """
    outlier_indices = []
    for col in features:
        Q1 = np.percentile(df[col], 25)
        Q3 = np.percentile(df[col], 75)
        IQR = Q3 - Q1
        outlier_step = 1.5 * IQR
        outlier_list_col = df[(df[col] < Q1 - outlier_step) | (df[col] > Q3 + outlier_step)].index
        outlier_indices.extend(outlier_list_col)
        outlier_indices = Counter(outlier_indices)
        return outlier_indices

Outliers_to_drop = detect_outliers(hotel, hot_num)
len(Outliers_to_drop)