Python Pandas Introduction
In this page:
import pandas as pd
series = pd.Series([item1, item2])
df = pd.read_csv("file.csv")
What is Pandas?
Pandas builds on NumPy to add labeled, heterogeneous data structures suited to real-world data analysis: Series for one labeled column of data, and DataFrame for a full two-dimensional table with both row and column labels -- think of it as a spreadsheet you can manipulate entirely in code.
Example: What is Pandas?
import pandas as pd
s = pd.Series([1, 2, 3]) # one labeled column of data
print(s)
Creating DataFrames
You typically construct a DataFrame from a dictionary of column-name-to-list mappings, from a list of row dictionaries, or by reading directly from a file with functions like pd.read_csv(). Each of these approaches ends up with the same table-like structure of labeled rows and columns.
Example: Creating DataFrames
import pandas as pd
df = pd.DataFrame({"name": ["Alex", "Sam"], "age": [30, 25]}) # dict of columns becomes a table
print(df)
Accessing Rows and Columns
Label-based indexing with .loc[] selects rows and columns by their actual labels, while .iloc[] selects by raw positional index regardless of what the labels say.
Confusing the two is a very common source of bugs, especially after a DataFrame's rows have been filtered or reordered.
Example: Accessing Rows and Columns
import pandas as pd
df = pd.DataFrame({"name": ["Alex", "Sam"], "age": [30, 25]})
print(df.loc[0]) # selects by label
print(df.iloc[0]) # selects by positional index
Data Description
.info() reports each column's dtype and non-null count, while .describe() computes summary statistics (mean, standard deviation, quartiles) for every numeric column at once.
Running both immediately after loading new data is a fast way to catch obviously wrong types or unexpected missing values.
Example: Data Description
import pandas as pd
df = pd.DataFrame({"age": [30, 25, 40]})
print(df.describe()) # mean, std, quartiles for every numeric column
Basic Filtering
Filtering rows with a boolean condition, like df[df[age] > 30], builds a mask over the DataFrame's index and returns only the matching rows -- the same pattern as NumPy's boolean indexing, extended to work naturally across a table's labeled columns.
Example: Basic Filtering
import pandas as pd
df = pd.DataFrame({"age": [30, 25, 40]})
print(df[df["age"] > 28]) # boolean mask keeps only matching rows
- Forgetting the conventional
import pandas as pd, then writingpd.DataFrame(...), which raisesNameError. - Confusing
df["a"]withdf[["a"]], since the first gives a Series and the second gives a DataFrame. - Using
df.locwith a position number where the label is expected, so it raisesKeyError; useilocfor positions.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB