Skip to content

Instantly share code, notes, and snippets.

View lauramsfernandes's full-sized avatar
🏠
Working from home

L. Fernandes lauramsfernandes

🏠
Working from home
View GitHub Profile
@lauramsfernandes
lauramsfernandes / df_categorical_variables.py
Last active May 30, 2021 15:17
DataFrame Categorial Variables
import pandas as pd
index = ['Ana', 'Ian', 'Eva', 'Dan', 'Kim']
columns = ['Color', 'Type', 'Length']
ana = ['Brown', 'Straight', 'Short']
ian = ['Black', 'Wavy', 'Short']
eva = ['Red', 'Curly', 'Long']
dan = ['Blonde', 'Straight', 'Long']
kim = ['Brown', 'Curly', 'Medium']
length_mapping ={'Short' : 1,
'Medium': 2,
'Long' : 3}
df['Length'] = df['Length'].map(length_mapping)
df
df = pd.get_dummies(df)
df = pd.get_dummies(df, drop_first=True)
reverse_length_mapping ={1 : 'Short',
2 : 'Medium',
3 : 'Long' }
df['Length'] = df['Length'].map(reverse_length_mapping)
df
reverse_length_mapping = {v: k for k, vi in length_mapping.items()}
df['Length'] = df['Length'].map(reverse_length_mapping)
df