In the previous lesson, you loaded a NASDAQ stock listing. The table contained many rows because each row represented one listed stock or security.
Now we will answer a very practical question: How do we find just one stock inside that large table?
We will search for Apple using its ticker symbol:
AAPL
This lesson introduces one new idea: select rows that match a condition.
What you will finish:
Python will load the NASDAQ listing and keep only the row whose
Symbol is AAPL.
1. Start with the NASDAQ Listing
Create a new file in your alphesta-lab folder:
find_one_stock.py
Start with:
import FinanceDataReader as fdr
listing = fdr.StockListing("NASDAQ")
print(listing[["Symbol", "Name"]].head())
You already know what this does:
StockListing("NASDAQ")
→ get the NASDAQ listing
listing[["Symbol", "Name"]]
→ show only Symbol and Name
.head()
→ show the first five rows
2. Store the Stock We Want to Find
Add:
target_symbol = "AAPL"
The variable name tells us what it contains:
target_symbol
= the ticker symbol we want to find
Today the target is Apple.
3. Look at the Symbol Column
Remember this expression from the previous lessons:
listing["Symbol"]
It means:
give me the Symbol column from listing
That column contains ticker symbols such as:
AAPL
MSFT
NVDA
...
4. Ask a True-or-False Question
Now add:
listing["Symbol"] == target_symbol
The double equal sign == does not store a value.
It compares two values.
=
→ assign a value
==
→ compare values
So this:
listing["Symbol"] == "AAPL"
asks the same question for every row:
Is this row's Symbol equal to AAPL?
Conceptually, the result looks like:
Symbol
AAL False
AMD False
AAPL True
AMZN False
...
Only the matching row becomes True.
5. Keep Only the True Row
Now place that condition inside square brackets:
match = listing[listing["Symbol"] == target_symbol]
Read it from the inside out.
listing["Symbol"] == target_symbol
→ find which rows match AAPL
listing[ ... ]
→ keep only those matching rows
match =
→ store the result in match
This is called filtering.
We started with many rows and kept only the row that satisfied our condition.
large NASDAQ listing
↓
Symbol == "AAPL"
↓
matching row only
6. Print the Result
Add:
print(match[["Symbol", "Name"]])
The result should contain Apple rather than the first few arbitrary NASDAQ rows.
The exact company-name formatting can vary with the current data source,
but the important result is that the AAPL row was selected.
7. Your Complete File
import FinanceDataReader as fdr
listing = fdr.StockListing("NASDAQ")
target_symbol = "AAPL"
match = listing[listing["Symbol"] == target_symbol]
print(match[["Symbol", "Name"]])
This is a short program, but an important one.
You have moved from simply looking at a table to asking the table a question.
8. Understand the Whole Expression
This line may look difficult at first:
match = listing[listing["Symbol"] == target_symbol]
Break it into building blocks:
1. listing
→ the full NASDAQ table
2. listing["Symbol"]
→ one column
3. listing["Symbol"] == target_symbol
→ True / False condition
4. listing[condition]
→ keep True rows
5. match
→ the smaller result table
Do not memorize the whole line as one strange formula. Understand the small pieces first.
9. Change One Input Yourself
Change:
target_symbol = "AAPL"
to:
target_symbol = "NVDA"
Run the file again.
Then try:
target_symbol = "MSFT"
The filtering logic does not change. Only the input changes.
same filter
+
different ticker
↓
different stock row
10. What If Nothing Is Found?
Try a symbol that is not in the listing:
target_symbol = "NOTAREALSTOCK"
The result may be an empty DataFrame.
That does not necessarily mean the program failed. It can simply mean:
no row matched the condition
This distinction will become important later when we build market screens.
11. Check Whether the Result Is Empty
pandas gives a DataFrame a property called empty.
Add:
if match.empty:
print("Symbol not found.")
else:
print(match[["Symbol", "Name"]])
Read this as:
if match is empty
→ print "Symbol not found."
otherwise
→ print the matching row
This is our first small example of making the program respond differently depending on the result.
12. Final Version
import FinanceDataReader as fdr
listing = fdr.StockListing("NASDAQ")
target_symbol = "AAPL"
match = listing[listing["Symbol"] == target_symbol]
if match.empty:
print("Symbol not found.")
else:
print(match[["Symbol", "Name"]])
Check Your Understanding
You are ready to move on if you can explain:
listing["Symbol"]selects the Symbol column.==compares values.- The comparison creates True and False results.
listing[condition]keeps the rows where the condition is True.match.emptytells us whether no rows were found.- You can change AAPL to NVDA or MSFT without changing the filtering logic.
What You Just Learned
NASDAQ listing
↓
Symbol column
↓
Symbol == "AAPL"
↓
True / False
↓
keep True rows
↓
Apple row
This is your first real filtering operation.
Later, the same idea can be expanded from “find this ticker” to questions such as “find stocks that meet these conditions.”
Where Do We Go Next?
We can now download data, read a DataFrame, load a market listing, and find one row inside that listing.
The next useful skill is learning how to save market data so that you can use it again without downloading it every time.
← Previous: How to Get a U.S. Stock Market Listing with FinanceDataReader
Next: How to Save and Reload Market Data with CSV in Python →