Numeric types like int64 and float64 help us store numbers in tables. They let us do math and analysis easily.
Numeric types (int64, float64) in 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).
import pandas as pd df = pd.DataFrame({'Age': [25, 30, 22]}) print(df.dtypes)
import pandas as pd df = pd.DataFrame({'Price': [10.5, 20.75, 15.0]}) print(df.dtypes)
import pandas as pd df = pd.DataFrame({'Score': [90.0, 85.5, 88.0]}) df['Score'] = df['Score'].astype('int64') print(df)
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.
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)
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.
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.
Practice
Solution
Step 1: Understand numeric types in pandas
int64 is used for integers (whole numbers), float64 for decimals.Step 2: Identify the type for whole numbers
Whole numbers require int64 because float64 stores decimals.Final Answer:
int64 -> Option CQuick Check:
Whole numbers = int64 [OK]
- Confusing float64 as default for all numbers
- Choosing object type for numeric data
- Using bool for numeric storage
df['col'] to float64 type?Solution
Step 1: Recall pandas method to change column type
The method to change a column's type isastype()called on the column.Step 2: Apply correct syntax
Usedf['col'].astype('float64')to convert the column to float64.Final Answer:
df['col'].astype('float64') -> Option DQuick Check:
astype() on column = correct syntax [OK]
- Calling astype() on the whole DataFrame incorrectly
- Using non-existent convert() method
- Wrong function syntax like astype(df['col'], 'float64')
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [1.0, 2.5, 3.5]})
df['A'] = df['A'].astype('float64')
print(df.dtypes)What will be the output?
Solution
Step 1: Check initial dtypes
Column 'A' starts as int64, 'B' as float64.Step 2: Convert 'A' to float64
Usingastype('float64')changes 'A' from int64 to float64.Final Answer:
A float64 B float64 dtype: object -> Option AQuick Check:
astype changes int64 to float64 [OK]
- Assuming original types remain unchanged
- Confusing int64 and float64 outputs
- Misreading print output format
import pandas as pd
df = pd.DataFrame({'num': [1, 2, 3]})
df['num'] = df['num'].astype(int64)Solution
Step 1: Identify the error in astype argument
int64 must be passed as a string 'int64', not as a bare name.Step 2: Understand Python name error
Without quotes, Python looks for a variable named int64 which is undefined.Final Answer:
int64 is not defined without quotes -> Option AQuick Check:
Use quotes around type names in astype() [OK]
- Passing type names without quotes
- Thinking astype() needs import
- Believing columns cannot be reassigned
df with a column 'values' containing numbers as strings, e.g., ['1', '2.5', '3']. You want to convert this column to numeric types efficiently, storing integers as int64 and decimals as float64 where needed. Which approach is best?Solution
Step 1: Understand mixed numeric strings
Strings include integers and decimals, so direct astype('float64') converts all to float.Step 2: Use pd.to_numeric with errors='coerce'
This converts strings to numbers, coercing errors to NaN, allowing safe conversion.Step 3: Convert integers to int64 after identifying them
After conversion, integers can be cast to int64 to save memory.Final Answer:
Use pd.to_numeric(df['values'], errors='coerce') and then convert integers to int64 -> Option BQuick Check:
pd.to_numeric + careful conversion handles mixed types best [OK]
- Converting all to float64 losing integer type
- Trying manual conversion for each value
- Ignoring errors in conversion causing crashes
Ready to prove it?
Pass the test and this pattern moves unknown
