0
0
Pandasdata~10 mins

String type (object, string) in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"string"
B"int"
C"float"
D"bool"
Attempts:
3 left
💡 Hint
Common Mistakes
Using dtype="object" instead of "string"
Using numeric dtypes like int or float for text data
2fill in blank
medium

Complete 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'
Aastype(float)
Bastype(int)
Castype(str)
Dastype(bool)
Attempts:
3 left
💡 Hint
Common Mistakes
Using astype(int) which converts to integer
Using astype(float) which converts to float
3fill in blank
hard

Fix 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'
Apd.StringDtype()
B"string"
Cstr
Dobject
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
4fill in blank
hard

Fill 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'
Aastype
Bstr
C"string"
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Passing str instead of "string" to astype()
Using int dtype for text data
5fill in blank
hard

Fill 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'
A"string"
Bastype
C"object"
D"int"
Attempts:
3 left
💡 Hint
Common Mistakes
Using dtype="object" to create the Series initially
Using wrong method name instead of astype