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
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()╔════════════╤═══════════════════╤═══════════════════╤═══════════════════╤═══════════════════╗
║ │ 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
Convert a specific column in a DataFrame to Dummy codes
Last updated
Was this helpful?