← Back to Python Course | Chapter 13: Data Science & Web | Lesson 3 of 14

Python Pandas Introduction

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?

python
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

python
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

python
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

python
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

python
import pandas as pd
df = pd.DataFrame({"age": [30, 25, 40]})
print(df[df["age"] > 28])

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.