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

Python Pandas DataFrame

A DataFrame is a table with labeled columns, like a spreadsheet inside your program. You can edit it, sort it, fill in blanks, and combine tables.
Syntax
python
import pandas as pd

df = pd.DataFrame({"column1": [a, b], "column2": [c, d]})
df["column"]
df.loc[row_label]

Editing Columns

Adding a new column is as simple as assigning to a new key, like df[total] = df[price] * df[qty], which computes element-wise across every row at once.

Modifying an existing column works the same way, and df.drop(columns=[...]) removes columns you no longer need.

Example: Editing Columns

python
import pandas as pd
df = pd.DataFrame({"price": [10, 20], "qty": [2, 3]})
df["total"] = df["price"] * df["qty"]  # new column computed element-wise
print(df)

Handling Missing Data

Real-world data is rarely complete: df.isna() flags missing values, df.dropna() removes rows or columns containing them, and df.fillna(value) replaces them with a specified value or a computed statistic like the column mean.

Choosing between dropping and filling depends on whether the missing data is rare enough to discard safely.

Example: Handling Missing Data

python
import pandas as pd
import numpy as np
df = pd.DataFrame({"value": [1, np.nan, 3]})  # np.nan represents a missing value
print(df.fillna(0))  # replaces missing values with 0

Sorting DataFrames

df.sort_values(column) reorders the DataFrame's rows based on the values in one or more specified columns, ascending by default, with a ascending=False option for descending order.

This is the DataFrame equivalent of Python's sorted(), but operating over full labeled rows instead of standalone values.

Example: Sorting DataFrames

python
import pandas as pd
df = pd.DataFrame({"score": [70, 90, 50]})
print(df.sort_values("score"))  # rows reordered ascending by score

Grouping Data

df.groupby(column) splits the DataFrame into groups sharing the same value in that column, and chaining an aggregation like .mean() or .sum() afterward computes that statistic separately within each group.

This split-apply-combine pattern is the core tool for summarizing data by category.

Example: Grouping Data

python
import pandas as pd
df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team").mean())  # average score computed separately per team

Merging and Joining

pd.concat() stacks multiple DataFrames together (by rows or columns), while pd.merge() joins them based on matching values in a shared key column, similar to a SQL join.

Merging is essential whenever related data is split across multiple tables that need to be combined for analysis.

Example: Merging and Joining

python
import pandas as pd
a = pd.DataFrame({"id": [1, 2], "name": ["Alex", "Sam"]})
b = pd.DataFrame({"id": [1, 2], "score": [90, 80]})
print(pd.merge(a, b, on="id"))  # joins the two tables on the shared id column
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming methods like df.sort_values() change the original, when they return a new DataFrame unless you assign it back.
  2. Ignoring missing data (NaN) and getting wrong averages or errors in later calculations.
  3. Chained assignment such as df[df.a > 1]["b"] = 0, which may not change the original and shows SettingWithCopyWarning; use df.loc.

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.