In [1]:
import pandas as pd

In [2]:
df = pd.read_csv('data/src/sample_header_index_dtype.csv', index_col=0)
print(df)


       a    b      c  d
ONE    1    1  100.0  x
TWO    2   20    NaN  y
THREE  3  300  300.0  z

In [3]:
print(df.dtypes)


a      int64
b      int64
c    float64
d     object
dtype: object

In [4]:
print(df.applymap(type))


                   a              b                c              d
ONE    <class 'int'>  <class 'int'>  <class 'float'>  <class 'str'>
TWO    <class 'int'>  <class 'int'>  <class 'float'>  <class 'str'>
THREE  <class 'int'>  <class 'int'>  <class 'float'>  <class 'str'>

In [5]:
df_str = pd.read_csv('data/src/sample_header_index_dtype.csv',
                     index_col=0, dtype=str)
print(df_str)


       a    b    c  d
ONE    1  001  100  x
TWO    2  020  NaN  y
THREE  3  300  300  z

In [6]:
print(df_str.dtypes)


a    object
b    object
c    object
d    object
dtype: object

In [7]:
print(df_str.applymap(type))


                   a              b                c              d
ONE    <class 'str'>  <class 'str'>    <class 'str'>  <class 'str'>
TWO    <class 'str'>  <class 'str'>  <class 'float'>  <class 'str'>
THREE  <class 'str'>  <class 'str'>    <class 'str'>  <class 'str'>

In [8]:
df_object = pd.read_csv('data/src/sample_header_index_dtype.csv',
                        index_col=0, dtype=object)
print(df_object)


       a    b    c  d
ONE    1  001  100  x
TWO    2  020  NaN  y
THREE  3  300  300  z

In [9]:
print(df_object.dtypes)


a    object
b    object
c    object
d    object
dtype: object

In [10]:
print(df_object.applymap(type))


                   a              b                c              d
ONE    <class 'str'>  <class 'str'>    <class 'str'>  <class 'str'>
TWO    <class 'str'>  <class 'str'>  <class 'float'>  <class 'str'>
THREE  <class 'str'>  <class 'str'>    <class 'str'>  <class 'str'>

In [11]:
print(df.astype(str).applymap(type))


                   a              b              c              d
ONE    <class 'str'>  <class 'str'>  <class 'str'>  <class 'str'>
TWO    <class 'str'>  <class 'str'>  <class 'str'>  <class 'str'>
THREE  <class 'str'>  <class 'str'>  <class 'str'>  <class 'str'>

In [12]:
df_col = pd.read_csv('data/src/sample_header_index_dtype.csv',
                     index_col=0, dtype={'a': float, 'b': str})
print(df_col)


         a    b      c  d
ONE    1.0  001  100.0  x
TWO    2.0  020    NaN  y
THREE  3.0  300  300.0  z

In [13]:
print(df_col.dtypes)


a    float64
b     object
c    float64
d     object
dtype: object

In [14]:
df_col = pd.read_csv('data/src/sample_header_index_dtype.csv',
                     index_col=0, dtype={1: float, 2: str})
print(df_col)


         a    b      c  d
ONE    1.0  001  100.0  x
TWO    2.0  020    NaN  y
THREE  3.0  300  300.0  z

In [15]:
print(df_col.dtypes)


a    float64
b     object
c    float64
d     object
dtype: object