Difference between dtype and converters in pandas.read_csv()

Let's understand the difference between dtype and converters in pandas.read_csv()?
Submitted by Pranit Sharma, on November 24, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Difference b/w dtype and converters in pandas.read_csv()

dtype is the name of the type of the variable which can be a dictionary of columns, whereas Convert is a dictionary of functions for converting values in certain columns here keys can either be integers or column labels.

There is also a semantic difference between dtype and converters. The difference is that dtype allows you to specify how to treat the values, for example, either as numeric or string type, on the other hand, converters allow you to pass your data to convert it to the desired dtype using a conversion function, for example, passing a string value to determine or to some other desired type.

Let us understand with the help of an example,

Python program to demonstrate the difference between dtype and converters in pandas.read_csv()

# Importing pandas package
import pandas as pd

# Import io
import io

# Define a string
t="""int,float,date,str
001,3.31,2015/01/01,005"""

# Read a csv
df = pd.read_csv(io.StringIO(t))

# Display all dtypes
print(df.dtypes)

# Now using dtype parameter and 
# setting each item as object
df = pd.read_csv(io.StringIO(t), dtype=object).info()

# Display new dtypes
print("\n","New Data Types\n",df,"\n")

# We can use converters to specifically 
# convert date to datetime
df = pd.read_csv(io.StringIO(t), converters={'date':pd.to_datetime}).info()

# Display new dtypes
print("\n","New Data Types\n",df,"\n")

Output

The output of the above program is:

Example: Difference between dtype and converters in pandas.read_csv()

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.