text_html_output_1.png
import operator

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

df = pd.read_csv('/kaggle/input/car-specification-dataset-1945-2020/Car Dataset 1945-2020.csv', low_memory=False)

dfcolumns = df.columns
dfcolumns_indexes = {i: dfcolumns[i] for i in range(len(dfcolumns))}

bad_data_indexes = [9, 11, 12, 13, 14, 15, 16, 17, 19, 20, 24, 27, 29, 33, 43, 44, 47, 49, 51, 69, 75, 77]
bad_cols = df.columns[bad_data_indexes]
bad_cols

df = pd.read_csv('/kaggle/input/car-specification-dataset-1945-2020/Car Dataset 1945-2020.csv', low_memory=False)
bad_float_types = set(df.columns)
categorical_columns = ['Make', 'drive_wheels', 'Series', 'boost_type', 'back_suspension', 'country_of_origin', 'presence_of_intercooler', 'Generation', 'car_class', 'Body_type', 'rear_brakes', 'rating_name', 'emission_standards', 'front_suspension', 'steering_type', 'injection_type', 'transmission', 'fuel_grade', 'cylinder_layout', 'front_brakes', 'Modle', 'engine_type', 'overhead_camshaft']
cateforical_columns_check = ['engine_placement', 'Trim', 'number_of_seats', 'wheel_size_r14', 'front_rear_axle_load_kg', 'maximum_torque_n_m']
weird_columns = ['turnover_of_maximum_torque_rpm', 'range_km']
time_coumns = ['safety_assessment']
for c in time_coumns:
    bad_float_types.discard(c)
for c in cateforical_columns_check:
    bad_float_types.discard(c)
for c in categorical_columns:
    bad_float_types.discard(c)
for c in weird_columns:
    bad_float_types.discard(c)
def is_numeric(value):
    try:
        float(value)
        return True
    except ValueError:
        return False
def cast_float_comma(value: str):
    return value.replace(',', '.') if ',' in value else value
def cast_float_space(value: str):
    return value.replace(' ', '') if ' ' in value else value
def cast_cylinder_bore_and_stroke_cycle_mm(value: str):
    if value == 'nan':
        return 'nan'
    if value:
        return 'x'.join([str(round(float(x.replace(',', '.')), 2)) for x in value.split('x')])
    else:
        return None
bad_comma_data = ['load_height_mm']
bad_space_data = ['clearance_mm']
bad_cylinder_bore_and_stroke_cycle_mm = ['cylinder_bore_and_stroke_cycle_mm']
bad_max_speed_km_per_h = ['max_speed_km_per_h']
bad_overhead_camshaft = ['overhead_camshaft']
bad_engine_hp_rpm = ['engine_hp_rpm']
bad_cargo_compartment_length_width_height_mm = ['cargo_compartment_length_width_height_mm']
for bad_col_name in bad_cargo_compartment_length_width_height_mm:
    df[bad_col_name] = df[bad_col_name].astype(str)
    prod_lambda = lambda x: str(reduce(operator.mul, (float(i) for i in x.split('x')), 1)) if x and len(x.split('x')) == 3 else 'nan'
    df[bad_col_name] = df[bad_col_name].apply(lambda x: prod_lambda(x))
for bad_col_name in bad_engine_hp_rpm:
    df[bad_col_name] = df[bad_col_name].astype(str)
    df[bad_col_name] = df[bad_col_name].apply(lambda x: 'nan' if '-' in x else x)
for bad_col_name in bad_max_speed_km_per_h:
    df[bad_col_name] = df[bad_col_name].astype(str)
    df[bad_col_name] = df[bad_col_name].apply(lambda x: 'nan' if x == 'km/h' else x)
for bad_col_name in bad_cylinder_bore_and_stroke_cycle_mm:
    df[bad_col_name] = df[bad_col_name].astype(str)
    df[bad_col_name] = df[bad_col_name].apply(cast_cylinder_bore_and_stroke_cycle_mm)
for bad_col_name in bad_space_data:
    df[bad_col_name] = df[bad_col_name].astype(str)
    df[bad_col_name] = df[bad_col_name].apply(cast_float_space)
for bad_col_name in bad_float_types:
    df[bad_col_name] = df[bad_col_name].astype(str)
    df[bad_col_name] = df[bad_col_name].apply(cast_float_comma)
non_float_ignore = set(['cylinder_bore_and_stroke_cycle_mm'])
for ii, bad_col_name in enumerate(bad_float_types):
    if bad_col_name in non_float_ignore:
        continue
    df[bad_col_name] = df[bad_col_name].astype(str)
    mask = df[bad_col_name].apply(is_numeric)
    non_numeric_values = df.loc[~mask, bad_col_name]
    if len(non_numeric_values) > 0:
        print(bad_col_name, ii)
        print(non_numeric_values)
        break
    df[bad_col_name] = df[bad_col_name].astype(float)
print('run without errors!')