Contents
Importing and Exporting Data
Every analysis starts by loading data and ends by saving results. This topic shows you how to read and write CSV, Excel, and JSON files using Pandas.
a. Reading CSV Files
CSV stands for Comma Separated Values. It is one of the most common formats for sharing data. Each line is a row, and commas separate the columns.
Pandas reads a CSV file with a single function.
df = pd.read_csv("sales.csv")
print(df.head())The function has many useful options. Use sep=";" if the file uses a different separator. Use usecols to load only certain columns, and nrows to read just a few rows for a quick test.
If you see strange characters in your data, try adding encoding="utf-8" or encoding="latin-1". Encoding problems are common when files come from different systems.
b. Working with Excel Files
Many business datasets are stored in Excel files. Pandas can read them with read_excel(). You may need to install the openpyxl library first.
df = pd.read_excel("report.xlsx", sheet_name="Sheet1")Use the sheet_name option to pick a sheet. If you do not give a name, Pandas reads the first sheet. You can also use header and skiprows when the table does not start in the first row.
Excel files can have merged cells, empty rows, and notes at the top. Always check your DataFrame with head() after loading. You may need a little cleanup before the data is ready.
c. Handling JSON Data
JSON is a common format for data on the web. APIs and many apps send data in JSON. It looks like a mix of Python dictionaries and lists.
df = pd.read_json("data.json")
print(df.head())Simple JSON files load easily. For nested JSON, where one value contains another set of values, you can use pd.json_normalize(). This flattens the nested parts into regular columns.
JSON is useful when you work with APIs, which you will see in the Intermediate level. Knowing how to read it now gives you a strong head start.
d. Saving Your Results
After cleaning or analyzing data, you often need to save it. Pandas makes this simple with to_csv() and to_excel().
df.to_csv("clean_sales.csv", index=False)
df.to_excel("clean_sales.xlsx", index=False)The index=False option stops Pandas from saving the row numbers as an extra column. Most of the time, you do not want that column in your file.
You can also save to JSON with to_json(). Choose the format based on who will use the file next. CSV is light and universal, while Excel is easier for non-technical teams.
Data Analysis with Python Handbook
Wesline Rinisha








