Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Checking data types in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Data Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of type checking with pandas
What is the output of this code snippet that checks data types of a pandas DataFrame?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [1.5, 2.5, 3.5],
    'C': ['x', 'y', 'z']
})
print(df.dtypes)
A
A      int64
B    int64
C     object
dtype: object
B
A      float64
B    float64
C     object
dtype: object
C
A      int32
B    float32
C     string
dtype: object
D
A      int64
B    float64
C     object
dtype: object
Attempts:
2 left
💡 Hint
Remember that integers default to int64 and floats to float64 in pandas.
❓ data_output
intermediate
2:00remaining
Count of data types in a DataFrame
What is the output of this code that counts the number of columns by data type in a pandas DataFrame?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'num1': [1, 2, 3],
    'num2': [4.0, 5.0, 6.0],
    'cat': ['a', 'b', 'c'],
    'flag': [True, False, True]
})
print(df.dtypes.value_counts())
A
int64      1
float64    2
object     1
dtype: int64
B
int64      2
float64    1
object     1
dtype: int64
C
int64      1
float64    1
object     1
bool       1
dtype: int64
D
int64      1
float64    1
object     2
dtype: int64
Attempts:
2 left
💡 Hint
Check the data types of each column carefully, including booleans.
🔧 Debug
advanced
2:00remaining
Identify the error in type checking code
What error does this code raise when trying to check data types of a list?
Data Analysis Python
data = [1, 2, 3, 4]
print(data.dtypes)
ATypeError: unsupported operand type(s) for dtypes
BAttributeError: 'list' object has no attribute 'dtypes'
CNameError: name 'dtypes' is not defined
DNo error, prints the data types of list elements
Attempts:
2 left
💡 Hint
Check if Python lists have a dtypes attribute.
🚀 Application
advanced
2:00remaining
Determine data types after conversion
After converting a pandas Series to numeric with errors='coerce', what is the data type of the resulting Series?
Data Analysis Python
import pandas as pd

s = pd.Series(['10', '20', 'abc', '30'])
s_num = pd.to_numeric(s, errors='coerce')
print(s_num.dtype)
Afloat64
Bint64
Cobject
Dstring
Attempts:
2 left
💡 Hint
Non-convertible values become NaN, which forces float type.
🧠 Conceptual
expert
2:00remaining
Understanding data types in mixed-type pandas DataFrame
Given a pandas DataFrame with columns of mixed types, which statement about the dtypes attribute is true?
AEach column's dtype reflects the most specific type that can hold all its values.
BThe dtypes attribute returns a list of Python built-in types for each column.
CAll columns are automatically converted to the same dtype for consistency.
DColumns with mixed types are stored as int64 by default.
Attempts:
2 left
💡 Hint
Think about how pandas stores data efficiently per column.

Practice

(1/5)
1. What does the DataFrame.dtypes attribute show in a pandas DataFrame?
easy
A. The data type of each column in the DataFrame
B. The number of rows in the DataFrame
C. The summary statistics of numeric columns
D. The first five rows of the DataFrame

Solution

  1. Step 1: Understand what dtypes means

    The dtypes attribute in pandas shows the data type of each column in a DataFrame.
  2. Step 2: Compare options with the definition

    Only The data type of each column in the DataFrame ('The data type of each column in the DataFrame') correctly describes this: it shows the data type of each column.
  3. Final Answer:

    The data type of each column in the DataFrame -> Option A
  4. Quick Check:

    DataFrame.dtypes = column types [OK]
Hint: Remember: dtypes means data types per column [OK]
Common Mistakes:
  • Confusing dtypes with DataFrame shape
  • Thinking dtypes shows data values
  • Mixing dtypes with summary statistics
2. Which of the following is the correct way to check data types of columns in a pandas DataFrame named df?
easy
A. df.types()
B. df.dtypes
C. df.type()
D. df.get_types()

Solution

  1. Step 1: Recall pandas syntax for data types

    In pandas, dtypes is an attribute (not a method), so it is accessed without parentheses.
  2. Step 2: Evaluate each option

    df.dtypes uses df.dtypes correctly. Options A, B, and C use incorrect method names or parentheses.
  3. Final Answer:

    df.dtypes -> Option B
  4. Quick Check:

    Use attribute dtypes without parentheses [OK]
Hint: Use df.dtypes without parentheses to check types [OK]
Common Mistakes:
  • Adding parentheses after dtypes
  • Using incorrect method names
  • Confusing dtypes with type() function
3. Given the following code:
import pandas as pd
data = {'age': [25, 30], 'name': ['Alice', 'Bob'], 'score': [88.5, 92.0]}
df = pd.DataFrame(data)
print(df.dtypes)

What will be the output?
medium
A. age int64 name object score float64 dtype: object
B. age float64 name object score int64 dtype: object
C. age int32 name string score float32 dtype: object
D. age object name object score object dtype: object

Solution

  1. Step 1: Check data types of each column in the dictionary

    Column 'age' has integers (25, 30) so type is int64 by default. 'name' has strings, so type is object. 'score' has floats (88.5, 92.0), so type is float64.
  2. Step 2: Confirm pandas default data types

    Pandas uses int64 for integers, object for strings, and float64 for floats by default.
  3. Final Answer:

    age int64 name object score float64 dtype: object -> Option A
  4. Quick Check:

    Integers=int64, Strings=object, Floats=float64 [OK]
Hint: Integers=int64, strings=object, floats=float64 in pandas [OK]
Common Mistakes:
  • Assuming strings have type 'string' instead of 'object'
  • Mixing int and float types
  • Confusing int32 with int64 default
4. You run this code:
import pandas as pd
data = {'A': [1, 2], 'B': ['x', 'y']}
df = pd.DataFrame(data)
print(df.dtypes())

But it gives an error. What is the problem?
medium
A. The DataFrame is empty
B. You need to import numpy first
C. The column names must be integers
D. You should use df.dtypes without parentheses

Solution

  1. Step 1: Identify the error cause

    The code uses df.dtypes() with parentheses, but dtypes is an attribute, not a method.
  2. Step 2: Correct the syntax

    Remove parentheses and use df.dtypes to get the data types without error.
  3. Final Answer:

    You should use df.dtypes without parentheses -> Option D
  4. Quick Check:

    dtypes is attribute, not method [OK]
Hint: No parentheses after dtypes attribute [OK]
Common Mistakes:
  • Adding parentheses after dtypes
  • Thinking DataFrame is empty causes error
  • Believing numpy import is needed for dtypes
5. You have a DataFrame df with columns: ['id', 'value', 'flag']. The flag column contains only 0 and 1 but is stored as integers. You want to convert flag to a boolean type for better memory use. Which code correctly changes the data type?
hard
A. df['flag'] = df['flag'].to_bool()
B. df['flag'] = bool(df['flag'])
C. df['flag'] = df['flag'].astype(bool)
D. df['flag'] = df['flag'].convert(bool)

Solution

  1. Step 1: Understand how to convert column types in pandas

    Use the astype() method to convert a column to a different data type.
  2. Step 2: Check each option's validity

    df['flag'] = df['flag'].astype(bool) uses astype(bool) correctly. df['flag'] = bool(df['flag']) tries to convert the whole Series to bool, which returns a single True/False, not a column. Options C and D use non-existent methods.
  3. Final Answer:

    df['flag'] = df['flag'].astype(bool) -> Option C
  4. Quick Check:

    Use astype(bool) to convert column to boolean [OK]
Hint: Use astype(bool) to convert column to boolean type [OK]
Common Mistakes:
  • Using bool() on entire Series instead of astype
  • Trying non-existent methods like to_bool()
  • Confusing convert() with astype()
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