0
0
Pandasdata~10 mins

Numeric types (int64, float64) in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Numeric types (int64, float64)
Create DataFrame with numbers
Check data types of columns
int64 for whole numbers
float64 for decimal numbers
Use types for calculations and memory
End
Start with a DataFrame, check column types, identify int64 for integers and float64 for decimals, then use these types for calculations.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [1.1, 2.2, 3.3]})
print(df.dtypes)
Create a DataFrame with integer and float columns, then print their data types.
Execution Table
StepActionDataFrame ContentColumn 'A' dtypeColumn 'B' dtypeOutput
1Create DataFrame{'A': [1, 2, 3], 'B': [1.1, 2.2, 3.3]}int64float64DataFrame created
2Check dtypesSame as aboveint64float64A int64 B float64 dtype: object
3Print dtypesSame as aboveint64float64Printed data types
4End---Execution complete
💡 All steps done, data types identified correctly for each column.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
dfNone{'A': [1, 2, 3], 'B': [1.1, 2.2, 3.3]}SameSame
df['A'].dtypeNoneint64int64int64
df['B'].dtypeNonefloat64float64float64
Key Moments - 2 Insights
Why is column 'A' int64 and not float64 even though pandas can store floats?
Because all values in column 'A' are whole numbers without decimals, pandas assigns int64 to save memory and keep data precise, as shown in execution_table step 2.
What happens if a float value is added to column 'A'?
The dtype of column 'A' would change to float64 to accommodate decimal numbers, since int64 cannot store decimals. This is implied by the difference between columns 'A' and 'B' in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 2, what is the data type of column 'B'?
Aobject
Bint64
Cfloat64
Dstring
💡 Hint
Check the 'Column 'B' dtype' column in execution_table row for step 2.
At which step does the DataFrame get created with the numeric columns?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when DataFrame is created.
If column 'A' had a decimal value, what would its dtype likely be?
Aint64
Bfloat64
Cobject
Dbool
💡 Hint
Compare dtypes of columns 'A' and 'B' in execution_table to understand how decimals affect dtype.
Concept Snapshot
pandas numeric types:
- int64: stores whole numbers
- float64: stores decimal numbers
- Use df.dtypes to check column types
- Types affect memory and calculations
- Mixing decimals in int column converts it to float64
Full Transcript
We start by creating a pandas DataFrame with two columns: 'A' containing whole numbers and 'B' containing decimal numbers. We then check the data types of each column using df.dtypes. Column 'A' is assigned the int64 type because it contains only whole numbers, while column 'B' is assigned float64 because it contains decimals. These types help pandas optimize memory and calculations. If a decimal number is added to column 'A', its type would change to float64 to accommodate decimals. This step-by-step execution helps understand how pandas assigns numeric types.