What a DataFrame is and why you need one

A DataFrame is a table of data in Python — rows and columns, like a spreadsheet or database table. The pandas library gives you the tools to build one, organize it, and work with it. If you have data scattered across files, databases, or lists, a DataFrame lets you load it all into one place, sort it, filter it, and do calculations across thousands or millions of rows without writing loops by hand.

Most data work in Python starts with a DataFrame. Whether you are analyzing sales numbers, processing sensor readings, or preparing information for a chart, you will build a DataFrame first. The pandas library is the standard tool — it comes with most Python installations, and if it does not, you can add it with a single command.

Key Takeaways

  • Import pandas at the top of your script with import pandas as pd, then create a DataFrame from a dictionary, list, or file using pd.DataFrame() or pd.read_csv().
  • A DataFrame from a dictionary uses column names as keys and lists of values as the data — each list must have the same length.
  • The pd.read_csv() function loads data directly from a CSV file into a DataFrame without typing out the data by hand.
  • Once you have a DataFrame, you can view it with print(), check its shape with .shape, and access columns by name using bracket notation.
  • Common mistakes include mismatched list lengths in dictionaries, forgetting to import pandas, and trying to access a column that does not exist.

Creating a DataFrame from a dictionary

The simplest way to build a DataFrame is from a dictionary. Each key becomes a column name, and each value is a list of data for that column. Here is the basic pattern:

Open a text editor or Python IDE, create a new file, and type this at the top:

import pandas as pd

Then create your DataFrame:

data = {     "name": ["Alice", "Bob", "Charlie"],     "age": [25, 30, 35],     "city": ["New York", "Los Angeles", "Chicago"] } df = pd.DataFrame(data) print(df)

When you run this, you will see a table with three columns (name, age, city) and three rows. The column names come from the dictionary keys, and the rows come from the lists. Every list must have the same number of items — if one list has three items and another has four, Python will throw an error.

Loading data from a CSV file

Most real data lives in files, not typed into your code. The pd.read_csv() function reads a CSV file (comma-separated values — the format Excel and Google Sheets export) and turns it into a DataFrame automatically.

If you have a file called sales.csv in the same folder as your Python script, load it like this:

import pandas as pd df = pd.read_csv("sales.csv") print(df)

The first row of the CSV file becomes the column names. Every row below that becomes a row in the DataFrame. You do not have to know how many rows there are or what the columns are called — read_csv() figures it out. If your CSV file is in a different folder, use the full path: pd.read_csv("/Users/yourname/Documents/sales.csv") on Mac or pd.read_csv("C:\\Users\\yourname\\Documents\\sales.csv") on Windows.

Creating a DataFrame from a list of lists

If your data is already organized as a list of lists, you can pass it directly to pd.DataFrame() and specify the column names separately:

import pandas as pd data = [     ["Alice", 25, "New York"],     ["Bob", 30, "Los Angeles"],     ["Charlie", 35, "Chicago"] ] df = pd.DataFrame(data, columns=["name", "age", "city"]) print(df)

Each inner list is one row. The columns parameter tells pandas what to name each column. Without it, pandas will name them 0, 1, 2, and so on, which is not useful. The order of column names must match the order of items in each row.

Checking your DataFrame and accessing data

Once you have built a DataFrame, you will want to look at it and pull out specific pieces. The print() function shows the whole table, but with large DataFrames that can be overwhelming. Use .head() to see just the first few rows:

print(df.head())

To see the shape of your DataFrame — how many rows and columns — use .shape:

print(df.shape)

This returns a pair of numbers like (3, 3), meaning 3 rows and 3 columns. To access a single column by name, use square brackets:

print(df["name"])

This shows just the name column as a list. To get a single cell, use .loc with the row number and column name:

print(df.loc[0, "name"])

This prints "Alice" — the name in row 0 (the first row, since Python counts from 0). Row numbers start at 0, not 1, which trips up many beginners.

Common mistakes and how to fix them

The most frequent error is mismatched list lengths in a dictionary. If you write:

data = {     "name": ["Alice", "Bob"],     "age": [25, 30, 35] }

The name list has 2 items but age has 3. Python will raise a ValueError saying the arrays must all be the same length. Fix it by making sure every list has the same number of items.

Another common mistake is forgetting to import pandas. If you write df = pd.DataFrame(data) without import pandas as pd at the top, you will get a NameError saying pd is not defined. Always put your imports at the very top of the file.

A third mistake is trying to access a column that does not exist. If you write df["salary"] but your DataFrame has no salary column, Python raises a KeyError. Check your column names with print(df.columns) to see exactly what they are called.

Frequently Asked Questions

What is the difference between a DataFrame and a regular Python list?

A list is just a sequence of items in order. A DataFrame is a table with named columns and rows, so you can access data by column name or row number. DataFrames also have built-in functions for sorting, filtering, and doing math across columns — things that would take many lines of code with a plain list.

Can I create a DataFrame with just one column?

Yes. Use data = {"name": ["Alice", "Bob", "Charlie"]} and df = pd.DataFrame(data). You will get a table with one column and three rows. You can also create a DataFrame from a single list by wrapping it in a dictionary.

What if my CSV file has headers in a different row?

Use the header parameter. If the column names are in row 5 instead of row 1, write pd.read_csv("file.csv", header=4) — remember that Python counts from 0, so row 5 is index 4. If your CSV has no header row at all, use header=None and pandas will name the columns 0, 1, 2, and so on.

How do I add a new column to an existing DataFrame?

Assign a list to a new column name: df["salary"] = [50000, 60000, 70000]. The list must have the same length as the number of rows in your DataFrame. You can also create a column from calculations on other columns, like df["age_plus_five"] = df["age"] + 5.

What if I want to save my DataFrame back to a CSV file?

Use the .to_csv() method: df.to_csv("output.csv", index=False). The index=False part tells pandas not to write the row numbers as a separate column. The file will be saved in the same folder as your script unless you specify a different path.