You'll rarely type data by hand — you read it from a file or database. Pandas has one-line readers for every common format, each returning a DataFrame ready to work with.
Reading files
df = pd.read_csv("students.csv")df = pd.read_excel("report.xlsx", sheet_name="Sheet1")df = pd.read_json("data.json")df = pd.read_sql("SELECT * FROM students", conn)
Useful read_csv options
df = pd.read_csv("students.csv",parse_dates=["enrolled"], # parse a date columnna_values=["N/A", "-"], # treat these as missingusecols=["name", "package"], # load only these columnsdtype={"id": str}) # force a column's type
Writing back out
df.to_csv("clean.csv", index=False) # index=False avoids an extra columndf.to_excel("out.xlsx", index=False)df.to_json("out.json", orient="records")
Key takeaways
- 01read_csv / read_excel / read_json / read_sql each return a DataFrame in one line.
- 02read_csv options handle real data: parse_dates, na_values, usecols, dtype.
- 03Write back with to_csv / to_excel / to_json.
- 04Use index=False on write unless the index is meaningful.
