Horje
How to Convert float64 Columns to int64 in Pandas?

float64 represents a floating-point number with double precision and int64 represents a 64-bit integer number. In this article, we will learn to Convert float64 Columns to int64 in Pandas using different methods

Convert float64 Columns to int64 in Pandas DataFrame

To transform a Pandas column to an integer type within a DataFrame, you have the option to utilize either the DataFrame’s astype(int) or the apply() method. This enables the conversion of a column from various data types such as float or string to an integer type, specifically int64 or int32.

1. Using astype() Method

We can use astype() method in Python to convert float64 Columns to int64 in Pandas.

In this example, we create a data frame with floating values and then we convert floating values into integer values using astype() function, This function converts the datatype to another datatype.

Python
import pandas as pd

# Create a sample DataFrame
data = {'A': [1.0, 2.0, 3.0],
        'B': [4.0, 5.0, 6.0]}
df = pd.DataFrame(data)

print("Before converting")
print(df.dtypes)

# Convert float64 columns to int64 using astype()
df['A'] = df['A'].astype('int64')
df['B'] = df['B'].astype('int64')

print("\nAfter converting")
# Check the data types of columns
print(df.dtypes)  

Output:

Before converting
A float64
B float64
dtype: object
After converting
A int64
B int64
dtype: object

As you can observe the float64 values in the dataset are converted to int64 datatype.

2. Using apply() and astype() function

We can use the apply() method along with the astype(int) function to convert the columns of the Pandas DataFrame to integers.

Python
import pandas as pd
data = {'A': [1.0, 2.4, 3.0],
        'B': [4.0, 5.0, 6.5]}

print("Before converting")
print(df.dtypes)
# Use apply method to convert Pandas columns to int
df_int = df.apply(lambda column: column.astype(int))

# Check the data types of columns
print("\nAfter converting")
print(df_int.dtypes)

Output:

Before converting
A float64
B float64
dtype: object
After converting
A int64
B int64
dtype: object

As you can observe the float64 values in the dataset are converted to int64 datatype.

Conclusion

In conclusion,we can Convert float64 Columns to int64 in Pandas within few clicks.

How to Convert float64 Columns to int64 in Pandas? – FAQs

How to Convert float64 to int in Python

In Python, you can convert a floating-point number to an integer using the int() function, which truncates the decimal part and converts the value to an integer:

# Example of converting float64 to int
float_num = 123.456
int_num = int(float_num)
print(int_num) # Output will be 123

How to Convert Data Type of Column from Float to Int in Pandas

In pandas, you can use the astype() method to change the data type of a DataFrame column from float to int. This is commonly needed when the floating-point numbers are actually whole numbers and you want to simplify the dataset:

import pandas as pd

# Example DataFrame with float numbers
df = pd.DataFrame({
'A': [1.0, 2.0, 3.0, 4.0]
})

# Convert the data type of column 'A' from float to int
df['A'] = df['A'].astype(int)
print(df)

How to Convert Object to int64 in Pandas

If a column in a pandas DataFrame is of type ‘object’ (usually strings or mixed types), and you know that it contains only numbers in string format, you can convert it to int64 using astype():

# DataFrame with object type column
df = pd.DataFrame({
'B': ['10', '20', '30', '40']
})

# Convert column 'B' from object to int64
df['B'] = df['B'].astype('int64')
print(df)

What is int64 in Pandas?

int64 is a data type in pandas that represents 64-bit integer values. It is capable of storing very large integers, and it is the standard integer type used in pandas for maintaining precision.

How to Convert String to int64 in Python

In Python, to convert a string to a 64-bit integer, you can still use the int() function. If specifically working with NumPy or pandas and you need to ensure the type is numpy.int64, you can use NumPy’s type casting:

import numpy as np

# Convert string to int64
string_val = "12345"
int_val = np.int64(string_val)
print(int_val, type(int_val))



Reffered: https://www.geeksforgeeks.org


Pandas

Related
Change String To Date In Pandas Dataframe Change String To Date In Pandas Dataframe
Convert Bytes To a Pandas Dataframe Convert Bytes To a Pandas Dataframe
How to Change Pandas Dataframe Datetime to Time How to Change Pandas Dataframe Datetime to Time
Convert A Datetime Object To Seconds Convert A Datetime Object To Seconds
Reading An Arff File To Pandas Dataframe Reading An Arff File To Pandas Dataframe

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15