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
- Get data: dict, list, CSV, Excel or SQL
- Build with pd.DataFrame(...) or pd.read_csv(...)
- Inspect with head(), info(), shape
- Select columns and rows with [], .loc, .iloc
- Add or transform columns with vectorised expressions
- 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
- Think of it as a spreadsheet or SQL table inside Python
- Can be built from dicts, lists of dicts, CSVs, SQL queries and more
- Each column is a Series with its own dtype
- .loc selects by label; .iloc selects by position
- Start every dataset with head(), info() and describe()