Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a pandas Series with string data type.
Pandas
import pandas as pd s = pd.Series(['apple', 'banana', 'cherry'], dtype=[1]) print(s.dtype)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dtype="object" instead of "string"
Using numeric dtypes like int or float for text data
✗ Incorrect
Using dtype="string" creates a Series with pandas string type.
2fill in blank
mediumComplete the code to convert a pandas Series to string type.
Pandas
import pandas as pd s = pd.Series([1, 2, 3]) s = s.[1]() print(s.dtype)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using astype(int) which converts to integer
Using astype(float) which converts to float
✗ Incorrect
Use astype(str) to convert a Series to string type.
3fill in blank
hardFix the error in the code to check if the Series has string dtype.
Pandas
import pandas as pd s = pd.Series(['a', 'b', 'c'], dtype='string') if s.dtype == [1]: print("Series is string type")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing dtype to the string "string" instead of pd.StringDtype()
Using str or object which are not the pandas string dtype
✗ Incorrect
Use pd.StringDtype() to compare dtype for pandas string type.
4fill in blank
hardFill both blanks to create a DataFrame column with string type and check its dtype.
Pandas
import pandas as pd df = pd.DataFrame({'name': ['Tom', 'Jane', 'Alice']}) df['name'] = df['name'].[1]([2]) print(df['name'].dtype)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing str instead of "string" to astype()
Using int dtype for text data
✗ Incorrect
Use astype("string") to convert the column to string dtype.
5fill in blank
hardFill all three blanks to create a Series with string dtype, convert it to object dtype, and print both dtypes.
Pandas
import pandas as pd s = pd.Series(['x', 'y', 'z'], dtype=[1]) s_obj = s.[2]([3]) print(s.dtype, s_obj.dtype)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dtype="object" to create the Series initially
Using wrong method name instead of astype
✗ Incorrect
Create a string dtype Series with "string", convert to object dtype with astype("object").