MulaiMulai sekarang secara gratis

Load all listing data and iterate over key-value dictionary pairs

You already know that a pd.DataFrame() object is a two-dimensional labeled data structure. As you saw in the video, the pd.concat() function is used to concatenate, or vertically combine, two or more DataFrames. You can also use broadcasting to add new columns to DataFrames.

In this exercise, you will practice using this new pandas function with the data from the NYSE and NASDAQ exchanges. pandas has been imported as pd.

Latihan ini adalah bagian dari kursus

Importing and Managing Financial Data in Python

Lihat Kursus

Petunjuk latihan

  • Import data in listings.xlsx from sheets 'nyse' and 'nasdaq' into the variables nyse and nasdaq. Read 'n/a' to represent missing values.
  • Inspect the contents of both DataFrames with .info() to find out how many companies are reported.
  • With broadcasting, create a new reference column called 'Exchange' holding the values 'NYSE' or 'NASDAQ' for each DataFrame.
  • Use pd.concat() to concatenate the nyse and nasdaq DataFrames, in that order, and assign to combined_listings.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Import the NYSE and NASDAQ listings
nyse = pd.____('listings.xlsx', ____='nyse', na_values='n/a')
nasdaq = pd.____('listings.xlsx', ____='nasdaq', na_values='n/a')

# Inspect nyse and nasdaq
nyse.____()
nasdaq.____()

# Add Exchange reference columns
nyse['____'] = 'NYSE'
nasdaq['____'] = 'NASDAQ'

# Concatenate DataFrames  
combined_listings = pd.____([____, ____]) 
Edit dan Jalankan Kode