Last updated

Reading Data

Definition: In real projects, data usually comes from a file. Pandas reads many formats — CSV, Excel, JSON, SQL — into a DataFrame with a single function like pd.read_csv.

How you would load a file

On your computer, reading a CSV file is one line:

# df = pd.read_csv("sales.csv")
# df = pd.read_excel("data.xlsx")
# df = pd.read_json("data.json")

Example — same structure from a dictionary

Here in the browser we build an equivalent table from a dictionary, so you can practise without a file:

import pandas as pd
df = pd.DataFrame({
  "product": ["Pen", "Book", "Bag", "Mug"],
  "price":   [2, 8, 25, 6],
  "qty":     [100, 40, 15, 60]
})
print(df)

Saving data back out

Just as easily, you can write a DataFrame to a file with df.to_csv("out.csv", index=False).

💡 Tip: whatever the source, once data is in a DataFrame, everything else in this course works the same way.

Try it Yourself
Output

          
Ad · responsive