Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pd column to one hot vector

import pandas as pd
        
df = pd.DataFrame({
          'A':['a','b','a'],
          'B':['b','a','c']
        })
df
Out[]: 
   A  B
0  a  b
1  b  a
2  a  c

# Get one hot encoding of columns B
one_hot = pd.get_dummies(df['B'])
# Drop column B as it is now encoded
df = df.drop('B',axis = 1)
# Join the encoded df
df = df.join(one_hot)
df  
Out[]: 
       A  a  b  c
    0  a  0  1  0
    1  b  1  0  0
    2  a  0  0  1
Comment

PREVIOUS NEXT
Code Example
Python :: python class optional attributes 
Python :: How to append variable in Python 
Python :: python pprint on file 
Python :: pydantic numpy ndarray type 
Python :: copy something character ubntil a specific character in python 
Python :: python string: indexing and slicing string 
Python :: metodo estatico de python 
Python :: Python - Comment vérifier une corde est vide 
Python :: Add one to a column pands 
Python :: wails get started 
Python :: arcpy save map layer to feature class 
Python :: print(s[::-1]) 
Python :: pubmed database python 
Python :: biggest number 
Python :: reverse every word from a sentence but maintain position 
Python :: #clearing all keys new key in python 
Python :: math is python 
Python :: np array specified lengte same value 
Python :: make n copies of item in list 
Python :: numpy online practice 
Python :: image processing for GC 
Python :: python record screen and audio 
Python :: reverse the order of list elements 
Python :: how to crack a 4 way handshake with python 
Python :: create empty polygon python 
Python :: python webscrapping downloading all the videos in a playlist 
Python :: what is horse riding sport name 
Python :: discord.py assign role 
Python :: iterating over the two ranges simultaneously and saving it in database 
Python :: code.org void loops 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =