Python Pandas Introduction
In this page:
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])
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]})
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])
print(df.iloc[0])
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())
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])
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