Advertisement
elena1234

column transformation, mapping and dummy variables in Python

Mar 22nd, 2023 (edited)
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. city_country = {"Paris":"France", "Mexico":"Mexico", "Montreal":"Canada", "Moscow":"Russia", "Barcelona":"Spain", "Athens": "Greece"}
  2. sample["Host_Country"] = sample.City.map(city_country)
  3.  
  4. mapper = {1:"First", 2:"Second", 3:"Third"}
  5. titanic.pclass = titanic.pclass.map(mapper)
  6.  
  7. titanic["alone"] = pd.Series(np.where(titanic.no_relat == 0, "Yes", "No"))
  8. titanic["child"] = pd.Series(np.where(titanic.age < 18, "Yes", "No"))
  9.  
  10. ############################################################################################
  11. # Creating Dummy Variables- from categorical to numeric data type
  12. titanic_d = pd.get_dummies(titanic, columns = ["sex", "pclass", "embarked", "age_cat"], drop_first=True)
  13.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement