How to Select Columns and Rows in Pandas DataFrames
Once your dataset is loaded into pandas, you’ll rarely need all of it at once. Instead, you’ll want to zoom in on the columns and rows that matter most.
In this tutorial, you’ll learn how to:
Select one column
Select multiple columns
Select rows by index
Select rows and columns together
Step 1: Example Dataset
import pandas as pd
data = {
"Country": ["Canada", "USA", "Mexico", "UK", "Germany"],
"Population": [38, 331, 128, 67, 83],
"Continent": ["North America", "North America", "North America", "Europe", "Europe"]
}
df = pd.DataFrame(data)
print(df)
Country | Population | Continent | |
---|---|---|---|
0 | Canada | 38 | North America |
1 | USA | 331 | North America |
2 | Mexico | 128 | North America |
3 | UK | 67 | Europe |
4 | Germany | 83 | Europe |
Step 2: Selecting a Single Column
To do this, you must specify the name of the DataFrame and the column from which you want to grab the data. In this example, we are grabbing data from the country column.
print(df["Country"])
This returns a Series (a single column).
Step 3: Selecting Multiple Columns
print(df[["Country", "Population"]])
👉 Why double brackets?
Single brackets → one column (Series).
Double brackets → multiple columns (DataFrame).
Step 4: Selecting Rows
With .loc[]
(label-based):
print(df.loc[0]) # First row
print(df.loc[1:2]) # Rows 1 and 2 (inclusive)
With .iloc[]
(position-based):
print(df.iloc[0]) # First row
print(df.iloc[0:3]) # Rows 0–2
Note: Python uses zero-indexing which means that the first row and column is set at 0.
Step 5: Rows + Columns Together
print(df.loc[0, "Population"]) # Population for Canada
print(df.loc[0:2, ["Country", "Population"]]) # First 3 rows, 2 columns
Quick Recap
Use
df["ColName"]
for one column.Use
df[["Col1", "Col2"]]
for multiple columns.Use
.loc[]
for labels,.iloc[]
for positions.Combine them to slice rows and columns at once.
✅ You now know how to grab exactly the data you need. Next, let’s learn how to save your DataFrame back into a CSV or Excel file so you can share or reuse your work.
👉 Read the next tutorial: Exporting a DataFrame to CSV or Excel