Links

danfo.getDummies

Convert categorical variable into dummy/indicator variables.
danfo.getDummies(kwargs)
Parameters
Type
Description
Default
data
Series or Dataframe
The data to dummify
​
options
Object
These includes:
columns: Array of column names to dummify. If not specified, all categorical columns are encoded.
prefixSeparator: String separator for created columns e.g "_",
prefix: Prefix for the new columns
{
prefixSeparator: "-"
}

Examples

Convert Series to Dummy codes

Node
Browser
const dfd = require("danfojs-node")
​
let datasf = ['pear', 'mango', "pawpaw", "mango", "bean"]
let sf1 = new dfd.Series(datasf)
​
let dum_df = dfd.getDummies(sf1, { prefix: "fruit" })
dum_df.print()
​
Output
╔════════════╀═══════════════════╀═══════════════════╀═══════════════════╀═══════════════════╗
║ │ fruit_pear │ fruit_mango │ fruit_pawpaw │ fruit_bean ║
╟────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 0 │ 1 │ 0 │ 0 │ 0 ║
╟────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 1 │ 0 │ 1 │ 0 │ 0 ║
╟────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 2 │ 0 │ 0 │ 1 │ 0 ║
╟────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 3 │ 0 │ 1 │ 0 │ 0 ║
╟────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 4 │ 0 │ 0 │ 0 │ 1 ║
╚════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

Convert all categorical columns in a DataFrame to Dummy codes

Node
Browser
const dfd = require("danfojs-node")
​
let data = { fruits: ['pear', 'mango', "pawpaw", "mango", "bean"],
Count: [20, 30, 89, 12, 30],
Country: ["NG", "NG", "GH", "RU", "RU"]}
​
let df = new dfd.DataFrame(data)
df.print()
​
let dum_df = dfd.getDummies(df)
dum_df.print()
​
Output
╔═══╀═══════════════════╀═══════════════════╀═══════════════════╗
║ │ fruits │ Count │ Country ║
╟───┌───────────────────┌───────────────────┌───────────────────╢
║ 0 │ pear │ 20 │ NG ║
╟───┌───────────────────┌───────────────────┌───────────────────╢
║ 1 │ mango │ 30 │ NG ║
╟───┌───────────────────┌───────────────────┌───────────────────╢
║ 2 │ pawpaw │ 89 │ GH ║
╟───┌───────────────────┌───────────────────┌───────────────────╢
║ 3 │ mango │ 12 │ RU ║
╟───┌───────────────────┌───────────────────┌───────────────────╢
║ 4 │ bean │ 30 │ RU ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
​
​
//after dummification
​
╔═══╀═══════════════════╀═══════════════════╀═══════════════════╀═══════════════════╀═══════════════════╀═══════════════════╀═══════════════════╀═══════════════════╗
║ │ Count │ fruits_pear │ fruits_mango │ ... │ fruits_bean │ Country_NG │ Country_GH │ Country_RU ║
╟───┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 0 │ 20 │ 1 │ 0 │ ... │ 0 │ 1 │ 0 │ 0 ║
╟───┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 1 │ 30 │ 0 │ 1 │ ... │ 0 │ 1 │ 0 │ 0 ║
╟───┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 2 │ 89 │ 0 │ 0 │ ... │ 0 │ 0 │ 1 │ 0 ║
╟───┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 3 │ 12 │ 0 │ 1 │ ... │ 0 │ 0 │ 0 │ 1 ║
╟───┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 4 │ 30 │ 0 │ 0 │ ... │ 1 │ 0 │ 0 │ 1 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝

Convert a specific column in a DataFrame to Dummy codes

Node
Browser
const dfd = require("danfojs-node")
​
let data = { fruits: ['pear', 'mango', "pawpaw", "mango", "bean"],
Count: [20, 30, 89, 12, 30],
Country: ["NG", "NG", "GH", "RU", "RU"]}
​
let df = new dfd.DataFrame(data)
df.print()
​
let dum_df = dfd.getDummies(df, { columns: ['fruits']})
dum_df.print()
​
Output
╔═══╀═══════════════════╀═══════════════════╀═══════════════════╗
║ │ fruits │ Count │ Country ║
╟───┌───────────────────┌───────────────────┌───────────────────╢
║ 0 │ pear │ 20 │ NG ║
╟───┌───────────────────┌───────────────────┌───────────────────╢
║ 1 │ mango │ 30 │ NG ║
╟───┌───────────────────┌───────────────────┌───────────────────╢
║ 2 │ pawpaw │ 89 │ GH ║
╟───┌───────────────────┌───────────────────┌───────────────────╢
║ 3 │ mango │ 12 │ RU ║
╟───┌───────────────────┌───────────────────┌───────────────────╢
║ 4 │ bean │ 30 │ RU ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╝
​
​
//after dummification
​
╔═══╀═══════════════════╀═══════════════════╀═══════════════════╀═══════════════════╀═══════════════════╀═══════════════════╗
║ │ Count │ Country │ fruits_pear │ fruits_mango │ fruits_pawpaw │ fruits_bean ║
╟───┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 0 │ 20 │ NG │ 1 │ 0 │ 0 │ 0 ║
╟───┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 1 │ 30 │ NG │ 0 │ 1 │ 0 │ 0 ║
╟───┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 2 │ 89 │ GH │ 0 │ 0 │ 1 │ 0 ║
╟───┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 3 │ 12 │ RU │ 0 │ 1 │ 0 │ 0 ║
╟───┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────┌───────────────────╢
║ 4 │ 30 │ RU │ 0 │ 0 │ 0 │ 1 ║
╚═══╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╧═══════════════════╝
See also LabelEncoder and OneHotEncoder​