When to apply(pd.to_numeric) and when to astype(np.float64)

Given a pandas dataframe, we have to learn when to apply(pd.to_numeric) and when to astype(np.float64). By Pranit Sharma Last updated : October 03, 2023

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.

There are multiple data types that are supported by pandas.

  • Int
  • Float
  • Object
  • Boolean
  • Datetime

apply(pd.to_numeric) and when to astype(np.float64)

All these data types can be converted into some other data types using the astype() method.

Technically, if we try to convert a string to a numerical value it will definitely either be converted to a numeric value or a nan value. The astype(float) method raises a value error in these types of situations and hence we need pandas.to_numeric() method in this case.

Let us understand with the help of an example,

Python program to demonstrate when to apply(pd.to_numeric) and when to astype(np.float64)

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating dictionary
d = {
    'a':[1,3,5,7,9],
    'b':['a','b','c','d','e'],
    'c':[0,0,0,0,0],
    'd':[0,0,0,0,0]
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame :\n",df,"\n")

# converting dtype of column b using astype()
df['b']= df['b'].astype(float)

Output

The output of the above program is:

Example 1: apply(pd.to_numeric) and when to astype(np.float64)

Here comes the use of pandas.to_numeric() method.

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating dictionary
d = {
    'a':[1,3,5,7,9],
    'b':['a','b','c','d','e'],
    'c':[0,0,0,0,0],
    'd':[0,0,0,0,0]
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame :\n",df,"\n")

# converting dtype of column b using astype()
df['b'] = pd.to_numeric(df['b'], errors='coerce')

# Display result
print("Modified DataFrame:\n",df)

Output

The output of the above program is:

Example 2: apply(pd.to_numeric) and when to astype(np.float64)

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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