Bird
Raised Fist0
Pandasdata~20 mins

Numeric types (int64, float64) in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Numeric Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of dtype after arithmetic operation
What is the dtype of the 'result' column after adding an int64 and a float64 column in pandas?
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': pd.Series([1, 2, 3], dtype='int64'),
    'B': pd.Series([0.1, 0.2, 0.3], dtype='float64')
})
df['result'] = df['A'] + df['B']
print(df['result'].dtype)
Aobject
Bint64
Cfloat64
Dint32
Attempts:
2 left
💡 Hint
Think about what happens when you add integers and floats in pandas columns.
❓ data_output
intermediate
2:00remaining
Count of float64 values after conversion
After converting a pandas Series of integers to float64, how many values remain float64?
Pandas
import pandas as pd

s = pd.Series([10, 20, 30], dtype='int64')
s = s.astype('float64')
print(s.dtype)
print(s.count())
Adtype: float64, count: 3
Bdtype: int64, count: 3
Cdtype: float64, count: 0
Ddtype: object, count: 3
Attempts:
2 left
💡 Hint
Check the dtype after astype conversion and count of non-null values.
🔧 Debug
advanced
2:00remaining
Identify the error in dtype conversion
What error will this code raise when converting a string column with non-numeric values to int64?
Pandas
import pandas as pd

df = pd.DataFrame({'col': ['1', '2', 'three']})
df['col'] = df['col'].astype('int64')
ATypeError: cannot convert float NaN to integer
BValueError: invalid literal for int() with base 10: 'three'
CKeyError: 'col'
DNo error, conversion succeeds
Attempts:
2 left
💡 Hint
Check what happens when a non-numeric string is converted to int.
❓ visualization
advanced
2:00remaining
Visualizing dtype distribution in DataFrame
Which code snippet correctly plots the count of int64 and float64 columns in a DataFrame?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'int_col': pd.Series([1, 2, 3], dtype='int64'),
    'float_col': pd.Series([1.1, 2.2, 3.3], dtype='float64'),
    'str_col': ['a', 'b', 'c']
})
Adf.value_counts().plot(kind='bar'); plt.show()
Bdf.dtypes.plot(kind='bar'); plt.show()
Cdf.select_dtypes(include=['int64', 'float64']).plot(kind='bar'); plt.show()
Ddf.dtypes.value_counts().loc[['int64', 'float64']].plot(kind='bar'); plt.show()
Attempts:
2 left
💡 Hint
Use dtypes and value_counts to count column types before plotting.
🧠 Conceptual
expert
2:00remaining
Memory usage difference between int64 and float64
Which statement about memory usage of int64 vs float64 columns in pandas is correct?
Aint64 and float64 columns use the same amount of memory per element
Bfloat64 columns use twice the memory of int64 columns per element
Cint64 columns use less memory because integers are stored more efficiently than floats
DMemory usage depends only on the number of elements, not the dtype
Attempts:
2 left
💡 Hint
Check the size in bytes of int64 and float64 types.

Practice

(1/5)
1. Which pandas numeric type is best for storing whole numbers without decimals?
easy
A. bool
B. float64
C. int64
D. object

Solution

  1. Step 1: Understand numeric types in pandas

    int64 is used for integers (whole numbers), float64 for decimals.
  2. Step 2: Identify the type for whole numbers

    Whole numbers require int64 because float64 stores decimals.
  3. Final Answer:

    int64 -> Option C
  4. Quick Check:

    Whole numbers = int64 [OK]
Hint: Whole numbers use int64, decimals use float64 [OK]
Common Mistakes:
  • Confusing float64 as default for all numbers
  • Choosing object type for numeric data
  • Using bool for numeric storage
2. Which of the following is the correct way to convert a pandas DataFrame column df['col'] to float64 type?
easy
A. df.astype('float64')['col']
B. df['col'].convert('float64')
C. astype(df['col'], 'float64')
D. df['col'].astype('float64')

Solution

  1. Step 1: Recall pandas method to change column type

    The method to change a column's type is astype() called on the column.
  2. Step 2: Apply correct syntax

    Use df['col'].astype('float64') to convert the column to float64.
  3. Final Answer:

    df['col'].astype('float64') -> Option D
  4. Quick Check:

    astype() on column = correct syntax [OK]
Hint: Use astype() on the column to change type [OK]
Common Mistakes:
  • Calling astype() on the whole DataFrame incorrectly
  • Using non-existent convert() method
  • Wrong function syntax like astype(df['col'], 'float64')
3. Given the code:
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?
medium
A. A float64 B float64 dtype: object
B. A int64 B float64 dtype: object
C. A object B float64 dtype: object
D. A float64 B int64 dtype: object

Solution

  1. Step 1: Check initial dtypes

    Column 'A' starts as int64, 'B' as float64.
  2. Step 2: Convert 'A' to float64

    Using astype('float64') changes 'A' from int64 to float64.
  3. Final Answer:

    A float64 B float64 dtype: object -> Option A
  4. Quick Check:

    astype changes int64 to float64 [OK]
Hint: astype changes column type; check dtypes after conversion [OK]
Common Mistakes:
  • Assuming original types remain unchanged
  • Confusing int64 and float64 outputs
  • Misreading print output format
4. What is wrong with this code snippet?
import pandas as pd
df = pd.DataFrame({'num': [1, 2, 3]})
df['num'] = df['num'].astype(int64)
medium
A. int64 is not defined without quotes
B. astype() cannot convert to int64
C. DataFrame column cannot be reassigned
D. Missing import for astype

Solution

  1. Step 1: Identify the error in astype argument

    int64 must be passed as a string 'int64', not as a bare name.
  2. Step 2: Understand Python name error

    Without quotes, Python looks for a variable named int64 which is undefined.
  3. Final Answer:

    int64 is not defined without quotes -> Option A
  4. Quick Check:

    Use quotes around type names in astype() [OK]
Hint: Always quote type names in astype() [OK]
Common Mistakes:
  • Passing type names without quotes
  • Thinking astype() needs import
  • Believing columns cannot be reassigned
5. You have a DataFrame 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?
hard
A. Use pd.to_numeric(df['values'], downcast='integer')
B. Use pd.to_numeric(df['values'], errors='coerce') and then convert integers to int64
C. Use df['values'] = df['values'].astype('float64') for all values
D. Manually check each value and convert to int64 or float64

Solution

  1. Step 1: Understand mixed numeric strings

    Strings include integers and decimals, so direct astype('float64') converts all to float.
  2. Step 2: Use pd.to_numeric with errors='coerce'

    This converts strings to numbers, coercing errors to NaN, allowing safe conversion.
  3. Step 3: Convert integers to int64 after identifying them

    After conversion, integers can be cast to int64 to save memory.
  4. Final Answer:

    Use pd.to_numeric(df['values'], errors='coerce') and then convert integers to int64 -> Option B
  5. Quick Check:

    pd.to_numeric + careful conversion handles mixed types best [OK]
Hint: Use pd.to_numeric with errors='coerce' for mixed numeric strings [OK]
Common Mistakes:
  • Converting all to float64 losing integer type
  • Trying manual conversion for each value
  • Ignoring errors in conversion causing crashes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard