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

Python Pandas Introduction

Pandas helps you work with tables of data, like a spreadsheet you control with code. You can look at rows and columns and filter what you need.
Syntax
python
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?

python
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

python
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

python
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

python
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

python
import pandas as pd
df = pd.DataFrame({"age": [30, 25, 40]})
print(df[df["age"] > 28])  # boolean mask keeps only matching rows
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. Forgetting the conventional import pandas as pd, then writing pd.DataFrame(...), which raises NameError.
  2. Confusing df["a"] with df[["a"]], since the first gives a Series and the second gives a DataFrame.
  3. Using df.loc with a position number where the label is expected, so it raises KeyError; use iloc for positions.

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.