Recall & Review
beginner
What does the
astype() method do in pandas?The
astype() method changes the data type of a pandas Series or DataFrame column to a specified type, like integer, float, or string.Click to reveal answer
beginner
How do you convert a column named 'age' in a DataFrame
df to integers using astype()?You use
df['age'] = df['age'].astype(int) to convert the 'age' column to integer type.Click to reveal answer
beginner
Can
astype() convert a column with numbers stored as strings to numeric types?Yes,
astype() can convert string numbers like '10' to numeric types like int or float, as long as the strings are valid numbers.Click to reveal answer
intermediate
What happens if you try to convert a column with non-numeric strings to a numeric type using
astype()?It will raise an error because
astype() cannot convert non-numeric strings to numbers directly.Click to reveal answer
intermediate
How can you convert multiple columns in a DataFrame to different types using
astype()?You can pass a dictionary to
astype() like df = df.astype({'col1': 'int', 'col2': 'float'}) to convert multiple columns at once.Click to reveal answer
What is the correct way to convert a pandas Series
s to float type?✗ Incorrect
The correct syntax is
s.astype(float) to convert the Series to float type.If a DataFrame column has values ['1', '2', 'three'], what happens when you run
df['col'].astype(int)?✗ Incorrect
Conversion fails because 'three' is not a numeric string, causing an error.
Which of these is a valid argument for
astype() to convert to string type?✗ Incorrect
You can use 'str', 'string', or the Python type str to convert to string type.
How do you convert multiple columns to different types in one step?
✗ Incorrect
Passing a dictionary to
astype() converts multiple columns at once.What type of object can
astype() be used on?✗ Incorrect
astype() works on both pandas Series and DataFrame objects.Explain how to use
astype() to convert a DataFrame column from string to integer type. Include what happens if the strings are not numeric.Think about what happens when conversion is impossible.
You got /3 concepts.
Describe how to convert multiple columns in a DataFrame to different data types using
astype().Use a dictionary inside astype()
You got /3 concepts.