Initialize With Datatype
Sat 17 May 2025
title: "Initalize With DataType" author: "Rj" date: 2019-04-24 description: "-" type: technical_note draft: false
import numpy as np
import pandas as pd
df = pd.DataFrame({'a': [7, 1, 5], 'b': ['3','2','1']}, dtype='object')
df
| a | b | |
|---|---|---|
| 0 | 7 | 3 |
| 1 | 1 | 2 |
| 2 | 5 | 1 |
df.dtypes
a object
b object
dtype: object
# Let's set the datatype as integer
df1 = pd.DataFrame({'a': [7, 1, 5], 'b': ['3','2','1']}, dtype='int')
/Users/rajacsp/anaconda3/envs/py36/lib/python3.6/site-packages/numpy/core/numeric.py:2591: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
return bool(asarray(a1 == a2).all())
df1
| a | b | |
|---|---|---|
| 0 | 7 | 3 |
| 1 | 1 | 2 |
| 2 | 5 | 1 |
type(df1)
pandas.core.frame.DataFrame
df1.dtypes
a int64
b int64
dtype: object
Score: 10
Category: data-wrangling