Initialize With Datatype
Sun 29 June 2025
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')
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: 5
Category: basics