- 연습 1
# Initialize an empty dictionary: counts_dict
counts_dict = {}
# Iterate over the file chunk by chunk
for chunk in pd.read_csv('tweets.csv', chunksize=10):
# Iterate over the column in DataFrame
for entry in chunk['lang']:
if entry in counts_dict.keys():
counts_dict[entry] += 1
else:
counts_dict[entry] = 1
# Print the populated dictionary
print(counts_dict)
- 연습 2
# Define count_entries()
def count_entries(csv_file, c_size, colname):
"""Return a dictionary with counts of
occurrences as value for each key."""
# Initialize an empty dictionary: counts_dict
counts_dict = {}
# Iterate over the file chunk by chunk
for chunk in pd.read_csv(csv_file, chunksize=c_size):
# Iterate over the column in DataFrame
for entry in chunk[colname]:
if entry in counts_dict.keys():
counts_dict[entry] += 1
else:
counts_dict[entry] = 1
# Return counts_dict
return counts_dict
# Call count_entries(): result_counts
result_counts = count_entries('tweets.csv', 10, 'lang')
# Print result_counts
print(result_counts)
- 연습 3
# Create a 5 x 5 matrix using a list of lists: matrix
matrix = [[col for col in range(5)] for row in range(5)]
# Print the matrix
for row in matrix:
print(row)
- 연습 4
# Create a list of strings: fellowship
fellowship = ['frodo', 'samwise', 'merry', 'aragorn', 'legolas', 'boromir', 'gimli']
# Create list comprehension: new_fellowship
new_fellowship = [member if len(member) >= 7
else '' for member in fellowship]
# Print the new list
print(new_fellowship)
- 연습 5
dict comprehension
# Create a list of strings: fellowship
fellowship = ['frodo', 'samwise', 'merry', 'aragorn', 'legolas', 'boromir', 'gimli']
# Create dict comprehension: new_fellowship
new_fellowship = { member:len(member) for member in fellowship }
# Print the new dictionary
print(new_fellowship)
'👩🏻💻 DataScientist for Python > DataCamp' 카테고리의 다른 글
Python Data Science Toolbox 1 - def function (0) | 2022.11.08 |
---|---|
Introduction to Data Visualization with Seaborn 2 (0) | 2022.10.25 |
Introduction to Data Visualization with Seaborn 1 (0) | 2022.10.24 |
Data Visualization with Matplotlib 4 - Annotating time-series data (0) | 2022.10.18 |
Data Visualization with Matplotlib 3 - Plotting time-series data (0) | 2022.10.18 |