TutorialsPythonpandas DataFrames

pandas DataFrames

The core pandas data structure — a table of rows and columns for all your data work

A DataFrame is a 2-dimensional table in pandas — rows and columns, like a spreadsheet. It is the central data structure in data analysis with Python. Almost every operation you do in pandas — filtering, sorting, grouping, merging — starts with a DataFrame. A DataFrame is essentially a dictionary of column names mapped to Series objects, all sharing the same index.

Examples

DataFrame essentials — the operations you use daily
import pandas as pd

df = pd.read_csv("employees.csv")

# First look at data
df.head()         # first 5 rows
df.tail(3)        # last 3 rows
df.shape          # (rows, columns) e.g. (500, 8)
df.dtypes         # data type of each column
df.info()         # shape + dtypes + null counts
df.describe()     # statistics for numeric columns

# Select columns
df["Name"]                    # one column → Series
df[["Name", "Salary"]]        # multiple → DataFrame

# Filter rows (boolean indexing)
df[df["Salary"] > 60000]
df[df["City"] == "Noida"]
df[(df["Salary"] > 60000) & (df["City"] == "Noida")]  # AND
df[(df["City"] == "Delhi") | (df["City"] == "Noida")] # OR

# Sort
df.sort_values("Salary", ascending=False)
df.sort_values(["City", "Salary"])

# New columns
df["Salary_LPA"] = df["Salary"] / 100000
df["Senior"] = df["Salary"] > 60000
loc and iloc — precise row and column selection
# loc — label-based: use column names and index labels
df.loc[0, "Name"]               # row 0, column "Name"
df.loc[0:4, ["Name","Salary"]]  # rows 0-4 (inclusive!), 2 cols
df.loc[df["City"]=="Noida", "Salary"]  # filtered rows, one col

# iloc — integer position-based: use numbers only
df.iloc[0]          # first row (all columns)
df.iloc[0, 2]       # row 0, column at position 2
df.iloc[:5, 1:4]    # first 5 rows, columns 1,2,3

# KEY DIFFERENCE:
# loc[0:4] includes row 4 (label-based, end inclusive)
# iloc[0:4] excludes row 4 (position-based, end exclusive)
💡 loc vs iloc confusion is one of the most common bugs for pandas beginners. Remember: loc = labels, iloc = integers.

Key Points

  • df.info() is the first thing to run on any new dataset — shows types and null counts
  • df.describe() gives count, mean, std, min, max, percentiles for numeric columns
  • Boolean indexing: df[df["col"] > value] — the primary filter method
  • Use & for AND, | for OR in pandas filters — not and/or (those are Python keywords)
  • loc is label-based (end inclusive); iloc is position-based (end exclusive)

Practice Question

Which pandas method gives you column names, data types, and null count all at once?