0
0
Pandasdata~10 mins

Chunked reading for large files 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 read a large CSV file in chunks using pandas.

Pandas
import pandas as pd
chunks = pd.read_csv('large_file.csv', chunksize=[1])
for chunk in chunks:
    print(chunk.head())
Drag options to blanks, or click blank then click option'
A-1
B10
C1000
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or negative numbers for chunksize causes errors.
Not using chunksize reads the whole file at once.
2fill in blank
medium

Complete the code to sum a column named 'sales' from each chunk.

Pandas
import pandas as pd
chunks = pd.read_csv('large_file.csv', chunksize=1000)
total_sales = 0
for chunk in chunks:
    total_sales += chunk['[1]'].sum()
print(total_sales)
Drag options to blanks, or click blank then click option'
Aquantity
Bsales
Cprice
Ddate
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong column name causes a KeyError.
Forgetting to sum each chunk's column.
3fill in blank
hard

Fix the error in the code to correctly read chunks and count rows.

Pandas
import pandas as pd
chunks = pd.read_csv('large_file.csv', chunksize=500)
total_rows = 0
for chunk in chunks:
    total_rows += len([1])
print(total_rows)
Drag options to blanks, or click blank then click option'
Achunk
Bpd
Cchunks
Dtotal_rows
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'chunks' inside the loop causes an error because 'chunks' is the iterator.
Using 'total_rows' inside len() is incorrect.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths for words longer than 3 letters.

Pandas
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
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)' for the value.
5fill in blank
hard

Fill all three blanks to create a filtered dictionary with uppercase keys and values greater than 0.

Pandas
data = {'a': 1, 'b': -2, 'c': 3, 'd': 0}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'k' instead of 'k.upper()' keeps keys lowercase.
Using '<' instead of '>' filters wrong values.