Installing pandas, and your first table
Pandas does not come with Python. Creating a virtual environment, installing with pip, verifying it, and building your first DataFrame by hand.
How do you want to learn?
The problem we are solving
A shop's takings for three months sit in a CSV file — twelve thousand rows. The question is a simple one: which product sold the most?
You could do it with the Python you already have. Open the file, read each line, split on the commas, convert to numbers, add them up one at a time. Forty lines of code, with a chance to get it wrong at every step.
In pandas it is two lines. Because pandas gives Python one thing it did not have before: a table. A whole grid of rows and columns is held as a single value, and adding, filtering and sorting it are each one operation.
One thing is worth being clear about first, though. print, len and input arrive with Python. Pandas does not. It has to be installed separately, and not understanding exactly where it gets installed is what strands people on day one with an error whose cause is hard to see.
By the end of this chapter you can
- Say what separates Python's own library from an outside package
- Create and activate a virtual environment for a project
- Install pandas with
pipand verify the version - Build and print your first
DataFrameby hand - Read a
ModuleNotFoundErrorand tell exactly what went wrong
Prerequisites: from the Python course, the terminal, folders and your first project and variables and types.
Pandas is not part of Python
What ships with Python is called the standard library — print, len, math, random all live there. Pandas does not. It is an outside package, written by other people and published to a repository called PyPI.
The tool that fetches it is pip, which does come with Python.
And that raises the first question, the one that causes trouble later if it is skipped: installed where?
Why a virtual environment
Type pip install pandas and pandas lands in the Python for your whole machine. For one project, that works. But six months from now, when a second project needs a different version of pandas, the two will be competing for the same place — and fixing one will break the other.
The answer is a separate room per project: a virtual environment. It is really just an ordinary folder inside the project holding that project's packages. Delete the project and the packages go with it, and the machine's main Python stays clean.
In the terminal, standing in your projects folder:
cd D:\python-lab
mkdir pandas-lab
cd pandas-lab
python -m venv .venvNow activate it:
.venv\Scripts\activateOnce active, the name of the room appears at the front of the prompt:
(.venv) PS D:\python-lab\pandas-lab>That(.venv)is the whole thing. If you cannot see it you are outside the environment, andpip installwill put the package somewhere else. Every new terminal needsactivateagain — this is not something you set once and forget.
If PowerShell complains that running scripts is disabled, run this once:
Set-ExecutionPolicy -Scope CurrentUser RemoteSignedTo leave, type deactivate.
Install, then verify
pip install pandasA few seconds later it finishes with Successfully installed .... Notice that NumPy came down alongside pandas — pandas uses NumPy underneath, and pip resolves those dependencies for you.
Now verify. Make a file called check.py:
import pandas as pd
print(pd.__version__)2.2.3Your number may differ, which is fine. What matters is that a version number printed at all — it means Python found pandas.
Two notes on that import pandas as pd line. import pandas brings pandas in; as pd gives it a short name. The name pd is not required, but virtually all pandas code in the world uses it — so writing anything else makes your code harder for other people to read. Take the convention.
A complete example
first_frame.py:
import pandas as pd
# A DataFrame from a dictionary: each key is a column name,
# each list is that column's values, top to bottom.
sales = pd.DataFrame({
"product": ["pen", "notebook", "bag"],
"quantity": [12, 5, 2],
"price": [15.0, 60.0, 850.0],
})
print(sales)product quantity price
0 pen 12 15.0
1 notebook 5 60.0
2 bag 2 850.0Look carefully at what was printed, because every part of it matters in the chapters that follow.
Along the top are the column names — product, quantity, price. Down the left are 0, 1, 2. Those are not a column; they are the index, the names of the rows. You did not write them in the dictionary; pandas supplied them, counting from zero.
And notice the 15.0 in the price column. You wrote 15.0, so the whole column prints as decimals, even though 850.0 could have been a whole number. Every value in a column shares one type — a central rule in pandas, and the main subject of the next chapter.
With the table in hand, asking it things is easy:
import pandas as pd
sales = pd.DataFrame({
"product": ["pen", "notebook", "bag"],
"quantity": [12, 5, 2],
"price": [15.0, 60.0, 850.0],
})
print(sales.shape)
print(sales["quantity"].sum())(3, 3)
19shape reports how big the table is — three rows, three columns, in that order. And sales["quantity"].sum() adds every value in one column. With twelve thousand rows the line would be exactly the same, and that is the reason to learn pandas at all.
When it breaks
ModuleNotFoundError: No module named 'pandas' Python could not find pandas. Almost always this means the place it was installed and the place the program is running from are not the same. Check the prompt for (.venv); if it is missing, activate and run again. With the environment active, pip list will show whether pandas is really there.
'pip' is not recognized as an internal or external command pip is not on PATH. Write python -m pip install pandas instead — that calls pip through the Python that is running, so it works without PATH and leaves no doubt about which Python is being installed into.
.venv\Scripts\activate : File cannot be loaded because running scripts is disabled A PowerShell security policy. Run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once, then try again in a new terminal.
Everything works, but the editor shows red underlines The editor is probably looking at a different Python. In VS Code, Ctrl + Shift + P → Python: Select Interpreter → choose the one in the project's .venv. If the program runs in the terminal, the code is fine — the red line is the editor's mistaken belief.
NameError when writing DataFrame It has to be pd.DataFrame, not DataFrame alone. import pandas as pd brings the whole library in under the name pd, so everything inside it is reached through pd..
Step 4 of 6 — Predict
Check your understanding
What does shape print, and in which order are the two numbers?
import pandas as pd
sales = pd.DataFrame({
"product": ["pen", "notebook", "bag"],
"quantity": [12, 5, 2],
"price": [15.0, 60.0, 850.0],
})
print(sales.shape)- A(3, 4)
- B(3, 3)
- C3 rows, 3 columns
- D(3,)
What does seeing (.venv) at the front of the prompt mean?
- APython is running in a special safe mode
- BYou are standing inside the folder called `.venv`
- CThe virtual environment is active in this terminal — `pip install` will now install into this project's room
- DPandas has finished installing
pip install pandas succeeded, but the next day a new terminal running the same file reports ModuleNotFoundError: No module named 'pandas'. What is the most likely cause?
- AThe installation actually failed
- BInstalled packages are wiped when the machine is shut down
- CThe file says `import pandas` instead of `import pandas as pd`
- DThe new terminal never activated the environment, so a different Python is running — one that has no pandas
Answering needs an account
Sign in to check your answers
The questions are above, and working them out in your head is the part that matters. Sign in to see the answers, the explanations and the three-level hints.
Your turn
Make a new folder, create your own virtual environment in it and install pandas, then write a file called stock.py that has:
- A
DataFrameof at least four products with three columns — one text, one whole number, one decimal - The whole table printed
shapeprinted, showing the number of rows and columns- The sum of both numeric columns printed
Then break it on purpose: type deactivate in the terminal to leave the environment and run the file again. Which error appears, and what exactly is it telling you?
Do not skip that last step. You will meet ModuleNotFoundError many times, and summoning it deliberately today means recognising its cause in one second next time.
Step 6 of 6
Stretch — the chapter quiz
Ten questions from easy to hard. The last ones are difficult on purpose.
Sign in to take the quiz