Skip to content
Python Data Science Toolbox (Part 2)
Python Data Science Toolbox (Part 2)
Run the hidden code cell below to import the data used in this course.
# Import the course packages
import pandas as pd
import matplotlib.pyplot as plt
# Import the course datasets
world_ind = pd.read_csv('datasets/world_ind_pop_data.csv')
tweets = pd.read_csv('datasets/tweets.csv')
Explore Datasets
Use the DataFrames imported in the first cell to explore the data and practice your skills!
- Create a
zip
object containing theCountryName
andCountryCode
columns inworld_ind
. Unpack the resultingzip
object and print the tuple values. - Use a list comprehension to extract the first 25 characters of the
text
column of thetweets
DataFrame provided that the tweet is not a retweet (i.e., starts with "RT"). - Create an iterable reader object so that you can use
next()
to readdatasets/world_ind_pop_data.csv
in chunks of 20.
List Comprehension
# list comprehension of a simple list
doctor = ['house', 'cuddy', 'chase', 'thirteen', 'wilson']
new_doc = [doc[0] for doc in doctor]
print(new_doc)
# We can build a list comprehension of a single object.
jean = '2350'
new_jean = [j[0] for j in jean]
print(new_jean)
# Create list comprehension: squares
squares = [i**2 for i in range(0,9)]
print(squares)
matrix = [[col for col in range(0,5)] for row in range(0,5)]
for row in matrix:
print(matrix)
# Create a list of strings: fellowship
fellowship = ['frodo', 'samwise', 'merry', 'aragorn', 'legolas', 'boromir', 'gimli']
# Create list comprehension: new_fellowship
new_fellowship = [member for member in fellowship if len(member) >= 7]
# Print the new list
print(new_fellowship)
# 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 member.replace(member,"") for member in fellowship]
# Print the new list
print(new_fellowship)
# 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)
Generator Expressions
Generator Expressions work with () instead of []. The output that we obtain is the same as list comprehensions but in this case is not a list, it is a generator object that allow us to generate different values or lists.
# List of strings
fellowship = ['frodo', 'samwise', 'merry', 'aragorn', 'legolas', 'boromir', 'gimli']
# List comprehension
fellow1 = [member for member in fellowship if len(member) >= 7]
# Generator expression
fellow2 = (member for member in fellowship if len(member) >= 7)
print(fellow2)