Skip to content

Python Data Science Toolbox (Part 2)

👋 Welcome to your new workspace! Here, you can experiment with the data you used in Python Data Science Toolbox (Part 2) and practice your newly learned skills with some challenges. You can find out more about DataCamp Workspace here.

Below is a code cell that imports the course packages and two datasets as pandas DataFrames.

🏃To execute the code, click inside the cell to select it and click "Run" or the ► icon. You can also use Shift-Enter to run a selected cell and automatically switch to the next cell.

# Import the course packages
import pandas as pd
import matplotlib.pyplot as plt

# Import the course datasets as DataFrames
world_ind = pd.read_csv('datasets/world_ind_pop_data.csv')
tweets = pd.read_csv('datasets/tweets.csv')

# Preview the first DataFrame
world_ind
tweets

1. Create a zip object containing the CountryName and CountryCode columns in world_ind. Unpack the resulting zip object and print the tuple values.

# Zip and unpack country names and country codes in world_ind
for z1,z2 in zip(world_ind["CountryName"],world_ind["CountryCode"]):
    print(z1,z2)
# or

z = zip (world_ind["CountryName"],world_ind["CountryCode"])
print(* z)

import numpy as np
z1 = zip (world_ind["CountryName"],world_ind["CountryCode"])
country,code = zip(* z1)
print(np.array(country) == world_ind["CountryName"])
country

2. Use a list comprehension to extract the first 25 characters of the text column of the tweets DataFrame provided that the tweet is not a retweet (i.e., starts with "RT").

# Use list comprehension to print the first 25 characters of tweets
comp= [i[0:25] for i in tweets["text"] if not i.startswith("RT")]
comp

3. Create an iterable reader object so that you can use next() to read datasets/world_ind_pop_data.csv in chunks of 20.

# Create an interable reader object to read chunks of datasets/world_ind_pop_data.csv
df_iterator = pd.read_csv("datasets/world_ind_pop_data.csv", chunksize =20)
for i in df_iterator:

Continue to Explore

Feeling confident about your skills? If you're following the Data Scientist with Python Career Track, continue to Intermediate Data Visualization with Seaborn! If you're interested in further developing your programming skills, you may be interested in other Python Programmer Career Track courses!