Skip to content

Instantly share code, notes, and snippets.

@Emekaborisama
Created November 7, 2022 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Emekaborisama/139ddc050b20c4a3c534bacf4a903510 to your computer and use it in GitHub Desktop.
Save Emekaborisama/139ddc050b20c4a3c534bacf4a903510 to your computer and use it in GitHub Desktop.
def convert_int(x):
"""convert float to int"""
try:
return int(x)
except:
pass
float_cols = train_df.select_dtypes("float64")
#convert mixed data type to int
new_train_df["reviews_per_month"] = new_train_df["reviews_per_month"].apply(convert_int)
# convert datetime str to pandas datetime so we can extract days, months and year.
new_train_df['last_review'] = pd.to_datetime(new_train_df['last_review'])
new_train_df['last_review_month'] =new_train_df['last_review'].dt.month
new_train_df['last_review_days'] = new_train_df['last_review'].dt.day
new_train_df['last_review_year'] = 2022 - new_train_df['last_review'].dt.year
del new_train_df['last_review']
#label encoding on nominal or categorical values
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
cat_columns = ['room_type']
new_train_df["room_type"] = le.fit_transform(new_train_df['room_type'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment