0
0
Pandasdata~5 mins

Numeric types (int64, float64) in Pandas

Choose your learning style9 modes available
Introduction

Numeric types like int64 and float64 help us store numbers in tables. They let us do math and analysis easily.

When you want to store whole numbers like counts or ages.
When you need to store decimal numbers like prices or measurements.
When you want to perform calculations like sums, averages, or differences.
When you want to check or change the type of numbers in your data.
When you want to save memory by choosing the right number type.
Syntax
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [1.5, 2.5, 3.5]})

# Check data types
df.dtypes

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

int64 means 64-bit integer (whole numbers).

float64 means 64-bit floating point (decimal numbers).

Examples
This shows the 'Age' column is int64 because it has whole numbers.
Pandas
import pandas as pd

df = pd.DataFrame({'Age': [25, 30, 22]})
print(df.dtypes)
This shows the 'Price' column is float64 because it has decimal numbers.
Pandas
import pandas as pd

df = pd.DataFrame({'Price': [10.5, 20.75, 15.0]})
print(df.dtypes)
This converts the 'Score' column from float64 to int64 by removing decimals.
Pandas
import pandas as pd

df = pd.DataFrame({'Score': [90.0, 85.5, 88.0]})
df['Score'] = df['Score'].astype('int64')
print(df)
Sample Program

This program creates a table with whole numbers and decimals. It shows the types, then changes the decimal column to whole numbers by rounding and converting the type.

Pandas
import pandas as pd

# Create a DataFrame with int64 and float64 columns
df = pd.DataFrame({
    'Count': [10, 20, 30],
    'Weight': [1.5, 2.3, 3.7]
})

# Show original data types
print('Original data types:')
print(df.dtypes)

# Convert 'Weight' to int64 by rounding
df['Weight'] = df['Weight'].round().astype('int64')

# Show updated DataFrame and data types
print('\nUpdated DataFrame:')
print(df)
print('\nUpdated data types:')
print(df.dtypes)
OutputSuccess
Important Notes

Changing float64 to int64 removes decimal parts, so be careful with rounding.

int64 and float64 use 64 bits, which means they can store very large numbers.

Use df.dtypes to check the types of your data columns.

Summary

int64 stores whole numbers, float64 stores decimal numbers.

Use astype() to change a column's numeric type.

Checking and setting numeric types helps with correct math and saving memory.