Pandas DataFrame

Python · Pandas Basics

Theory

A DataFrame is a two-dimensional labelled table: rows have an index, columns have names, and each column is a Series with its own dtype. It is pandas' main structure and works much like a spreadsheet or SQL table inside Python. pd.DataFrame({'name': ['Asha', 'Ravi'], 'salary': [95000, 72000]}) builds one from a dict where each key becomes a column.

You can also build one from a list of dicts (one dict per row) or load one from files and databases with pd.read_csv(), pd.read_excel() or pd.read_sql(). df['salary'] selects a column, df[['name', 'salary']] several columns, df.loc[0] a row by label and df.iloc[0] a row by position. New columns come from vectorised expressions, e.g. df['monthly'] = df['salary'] // 12.

Why we use it

Real data is tabular with mixed types - text, numbers, dates. A DataFrame holds it all with labels and gives hundreds of fast methods for cleaning, filtering, joining and summarising in a few lines.

When to use it

Use a DataFrame for any table-shaped data that fits in memory. For pure numeric matrices NumPy is lighter; for data far larger than memory, tools like Spark, Polars or a database such as Snowflake are better suited.

How it works

  1. Get data: dict, list, CSV, Excel or SQL
  2. Build with pd.DataFrame(...) or pd.read_csv(...)
  3. Inspect with head(), info(), shape
  4. Select columns and rows with [], .loc, .iloc
  5. Add or transform columns with vectorised expressions
  6. Clean table ready for analysis

Syntax

pd.DataFrame(dict_of_lists)
pd.DataFrame(list_of_dicts)

Example

df = pd.DataFrame({'name': ['Asha','Ravi'], 'salary': [95000, 72000]})
print(df)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic