JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

How to Convert Floating Point Numbers to Integers in Pandas DataFrame

Author:JIYIK Last Updated:2025/05/03 Views:

We will demonstrate methods to convert floating point numbers to integers in Pandas DataFrame - astype(int)and to_numeric()methods.

First, we NumPycreate a random array using the library and then convert it to DataFrame.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(5, 5) * 5)
print(df)

If you run this code, you will get the following output, with data types as follows float.

0 1 2 3 4
00.3024483.5519583.8786602.3803524.741592
14.0541870.9409520.4590584.3148010.524993
22.8917334.9268854.9557732.6263734.144166
31.1276393.1968234.1440201.3506320.401138
41.4235372.0194553.0389450.4366573.823888

astype(int)Convert floating point numbers floatto integers in Pandas usingint

To floatconvert to INT, we will use the method Pandasprovided by the package astype(int). The code is,

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(5, 5) * 5)
print("*********** Random Float DataFrame ************")
print(df)
print("***********************************************")
print("***********************************************")
print("*********** Dataframe Converted into INT ************")
print(df.astype(int))
print("***********************************************")
print("***********************************************")

Output:

*********** Random Float DataFrame ************
0 1 2 3 4
01.5106181.0945614.1574194.4241954.872719
10.4576804.0029592.6609991.6869160.840159
21.7817783.8129240.5618270.5323280.752800
31.4565142.7749552.7001184.5033544.749377
42.2235204.8592380.4509773.2284442.541648
***********************************************
***********************************************
*********** Dataframe Converted into INT ************
 01234
011444
104210
213000
312244
424032
***********************************************
***********************************************

We can use df.round(0).astype(int)to floatround the value to int.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(5, 5) * 5)
print("*********** Random Float DataFrame ************")
print(df)
print("***********************************************")
print("***********************************************")
print("*********** Dataframe Converted into INT ************")
print(df.astype(int))
print("***********************************************")
print("***********************************************")
print("*********** Rounding Float value to INT ************")
print(df.round(0).astype(int))
print("***********************************************")
print("***********************************************")

After running the code, we will get the following output.

*********** Random Float DataFrame ************
0 1 2 3 4
03.3944580.0712023.3132054.7858780.705612
11.9603530.8420851.2123320.8183432.637940
24.4078114.2390781.4190271.5263101.043394
33.6542164.6729721.7964393.1684260.734009
40.8487961.0240521.8593190.8443781.747628
***********************************************
***********************************************
*********** Dataframe Converted into INT ************
 01234
030340
110102
244111
334130
401101
***********************************************
***********************************************
*********** Rounding Float value to INT ************
 01234
030351
121113
244121
345231
411212
***********************************************
***********************************************

to_numeric()floatMethod converts Pandas toint

This method provides the ability to safely convert non-numeric types (such as strings) to a suitable numeric type. Takes a single series and converts to numbers, coercing when told to

s = pd.Series(["1.0", "2", -3])
print(pd.to_numeric(s, downcast="integer"))

After running the code, we will get the following output.

01
12
2 -3
dtype: int8

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert Tensor to NumPy array in Python

Publish Date:2025/05/03 Views:85 Category:Python

This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra

Saving NumPy arrays as images in Python

Publish Date:2025/05/03 Views:193 Category:Python

In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond

Transposing a 1D array in NumPy

Publish Date:2025/05/03 Views:98 Category:Python

Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra

Find the first index of an element in a NumPy array

Publish Date:2025/05/03 Views:58 Category:Python

In this tutorial, we will discuss how to find the first index of an element in a numpy array. Use where() the function to find the first index of an element in a NumPy array The function in the numpy module where() is used to return an arra

Remove Nan values from NumPy array

Publish Date:2025/05/03 Views:118 Category:Python

This article discusses some built-in NumPy functions that you can use to remove nan values. Remove Nan values ​​using logical_not() and methods in NumPy isnan() logical_not() is used to apply logical NOT to the elements of an array. isn

Normalizing a vector in Python

Publish Date:2025/05/03 Views:51 Category:Python

A common concept in the field of machine learning is to normalize a vector or dataset before passing it to the algorithm. When we talk about normalizing a vector, we say that its vector magnitude is 1, being a unit vector. In this tutorial,

Calculating Euclidean distance in Python

Publish Date:2025/05/03 Views:128 Category:Python

In the world of mathematics, the shortest distance between two points in any dimension is called the Euclidean distance. It is the square root of the sum of the squares of the differences between the two points. In Python, the numpy, scipy

Element-wise division in Python NumPy

Publish Date:2025/05/03 Views:199 Category:Python

This tutorial shows you how to perform element-wise division on NumPy arrays in Python. NumPy Element-Wise Division using numpy.divide() the function If we have two arrays and want to divide each element of the first array with each element

Convert 3D array to 2D array in Python

Publish Date:2025/05/03 Views:79 Category:Python

In this tutorial, we will discuss the methods to convert 3D array to 2D array in Python. numpy.reshape() Convert 3D array to 2D array using function in Python [ numpy.reshape() Function](numpy.reshape - NumPy v1.20 manual)Changes the shape

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial