dimanche 23 mai 2021

Create a new column from existing columns with the if conditions

Qestion: Create a new column called is_Diesel from Fuel_type with the conditions: if Fuel_type is Diesel, the value is 1, otherwise the value is 0.

My method 1:

car_df["is_Diesel"]= 1
fail_indicate = df['Fuel_type'] != "Diesel"
car_df.loc[fail_indicate,'is_Diesel'] = 0

KeyError: 'Fuel_type'

My method 2:

car_df['is_Diesel'] = car_df.loc[df['is_Diesel'] == 'Diesel'] = '1'
car_df['is_Diesel'] = car_df.loc[df['is_Diesel'] != 'Diesel'] = '0'

IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

Datasets:https://dl.dropboxusercontent.com/s/v9gmgxupkypn5dw/train-data.csv

Anyone can help me please! Thank you so much!

Aucun commentaire:

Enregistrer un commentaire