DataFrame.asType

Cast column of a DataFrame to a specified dtype.

danfo.DataFrame.asType(options)

Parameters
Type
Description
Default

options

Object

{

column: String, label/column name of column to cast

dtype: dtype to cast to. One of [string, float32, int32, boolean]

inplace: Boolean indicating whether to perform the operation inplace or not. Defaults to false

}

{ inplace: false }

Examples

Cast a float dtype column to int

const dfd = require("danfojs-node")

let data = { "A": [-20.1, 30, 47.3, -20] ,
             "B": [34, -4, 5, 6],
             "C": [20.1, -20.23, 30.3, 40.11],
             "D": ["a", "b", "c", "c"] }

let df = new dfd.DataFrame(data)
df.print()
df.ctypes.print()

let df_new = df.asType("A", "int32")
df_new.print()

df.ctypes.print()

Casting a string column of numbers to int

Note: Casting a string column of alphabets/words to numeric form will return NaNs as values

Last updated

Was this helpful?