0
0
Pandasdata~10 mins

value_counts() for frequency 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 count the frequency of each fruit in the list.

Pandas
import pandas as pd
fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
counts = pd.Series(fruits).[1]()
print(counts)
Drag options to blanks, or click blank then click option'
Aunique
Bcount
Cvalue_counts
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which counts non-null values but not frequencies.
Using unique() which returns unique values but not counts.
2fill in blank
medium

Complete the code to get the frequency of each color in the DataFrame column 'color'.

Pandas
import pandas as pd
data = {'color': ['red', 'blue', 'red', 'green', 'blue', 'blue']}
df = pd.DataFrame(data)
color_counts = df['color'].[1]()
print(color_counts)
Drag options to blanks, or click blank then click option'
Avalue_counts
Bunique
Ccount
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which counts non-null entries but not frequencies.
Trying to use value_counts() on the whole DataFrame instead of a column.
3fill in blank
hard

Fix the error in the code to correctly count the frequency of each animal in the list.

Pandas
import pandas as pd
animals = ['cat', 'dog', 'cat', 'bird', 'dog', 'dog']
counts = pd.Series(animals).[1]()
print(counts)
Drag options to blanks, or click blank then click option'
Aunique
Bcount
Ccount_values
Dvalue_counts
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method count_values().
Using count() which does not count frequencies.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

Pandas
words = ['data', 'science', 'ai', 'ml', 'python']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) for the value.
Using < instead of > in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words and their counts for words with count greater than 1.

Pandas
words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
counts = pd.Series(words).value_counts()
result = [1]: [2] for [3], [2] in counts.items() if [2] > 1}
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Bcount
Cword
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using item instead of word as the loop variable.
Using the count variable name inconsistently.