Table of Contents

				
					import pandas as pd
				
			
				
					d = {'sales': [100000,222000,1000000,522000,111111,222222,1111111,20000,75000,90000,1000000,10000],
      'city': ['Tampa','Tampa','Orlando','Jacksonville','Miami','Jacksonville','Miami','Miami','Orlando','Orlando','Orlando','Orlando'],
      'size': ['Small', 'Medium','Large','Large','Small','Medium','Large','Small','Medium','Medium','Medium','Small',]}
				
			
				
					df = pd.DataFrame(data=d)
				
			
				
					df
				
			
				
					from sklearn.preprocessing import OneHotEncoder
				
			
				
					from sklearn.preprocessing import OrdinalEncoder
				
			
				
					ohe = OneHotEncoder(sparse_output=False)
ode = OrdinalEncoder()
				
			
				
					from sklearn.compose import make_column_transformer
				
			
				
					ct = make_column_transformer(
    (OneHotEncoder, ['city']),  
    (OrdinalEncoder, ['size']),
    remainder='drop')   
				
			
				
					ct.set_output(transform="pandas")
				
			
				
					df_pandas = ct.fit_transform(df)
				
			
				
					df_pandas
				
			
#drop
				
					ct2 = make_column_transformer(
    (ohe, [1]),  
    (ode, [2]),
    sparse_threshold=0,
    remainder='drop')   
				
			
				
					ct2.set_output(transform="pandas")
				
			
				
					df_pandas2 = ct2.fit_transform(df)
				
			
				
					df_pandas2
				
			
#Example Passthrough some columns, drop offthers
				
					ct3 = make_column_transformer(
    (ohe, ['city']),  
    ('passthrough', ['size']),
    sparse_threshold=0,
    remainder='drop') 
				
			
				
					ct3.set_output(transform="pandas")
				
			
				
					df_pandas3 = ct3.fit_transform(df)
				
			
				
					df_pandas3
				
			

Free Community

Join 1,000+ AI Automation Builders

Weekly tutorials, live calls & direct access to Ryan & Matt.

Join Free →

Keep Learning

Kaggle House price prediction Regression Analysis

train_df = train_df.drop(columns=['PoolQC', 'MiscFeature', 'Alley', 'Fence', 'GarageYrBlt', 'GarageCond', 'BsmtFinType2']) test_df = test_df.drop(columns=['PoolQC', 'MiscFeature', 'Alley', 'Fence', 'GarageYrBlt', 'GarageCond', 'BsmtFinType2']) #drop GarageArea or GarageCars...

kaggle titanic tutorial

https://www.kaggle.com/code/ryannolan1/titanic-voting-classifier-0-78947?scriptVersionId=149342442&cellId=2https://www.kaggle.com/code/ryannolan1/titanic-voting-classifier-0-78947?scriptVersionId=149342442&cellId=2 #military - Capt, Col, Major #noble - Jonkheer, the Countess, Don, Lady, Sir #unmaried Female - Mlle, Ms, Mme #NEW Drop...

hyperparameter tuning with scikit learn

We would be looking at tuning hyperparameters with Scikit-Learn. Scikit-Learn is a powerful machine learning library for Python. It provides simple ,...

principal component analysis scikit learn

PCA (Principal Component Analysis) in Python using Scikit-learn is a technique used to reduce the number of features in a dataset while...