0
0
Pandasdata~10 mins

shape for dimensions 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 get the shape of the DataFrame.

Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
shape = df.[1]
Drag options to blanks, or click blank then click option'
Asize
Bshape
Clen
Dcolumns
Attempts:
3 left
💡 Hint
Common Mistakes
Using size returns total elements, not shape.
Using len(df) returns number of rows only.
2fill in blank
medium

Complete the code to print the number of rows in the DataFrame.

Pandas
import pandas as pd

df = pd.DataFrame({'X': [5, 6, 7], 'Y': [8, 9, 10]})
rows = df.[1][0]
print(rows)
Drag options to blanks, or click blank then click option'
Asize
Bindex
Ccolumns
Dshape
Attempts:
3 left
💡 Hint
Common Mistakes
Using df.columns[0] returns the first column name, not number of rows.
Using df.size returns total elements, not rows.
3fill in blank
hard

Fix the error in the code to get the number of columns.

Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
cols = df.shape[1]1
print(cols)
Drag options to blanks, or click blank then click option'
A.
B(
C[
D{
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses causes a TypeError.
Using dot notation is invalid for tuple indexing.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, only for words longer than 3 letters.

Pandas
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > filters wrong words.
Using word instead of len(word) gives the word itself as value.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, only for words longer than 3 letters.

Pandas
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of word.upper() for keys.
Using < instead of > filters wrong words.