{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "0r5hCx60Yv0u" }, "outputs": [], "source": [ "import pandas as pd\n" ] }, { "cell_type": "code", "source": [ "#2. Reading a CSV File" ], "metadata": { "id": "dytrA9JLZJaO" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "\n", "try:\n", " # Read the CSV file into a DataFrame\n", " df = pd.read_csv(r\"/content/sample_data (1).csv\")\n", " print(df)\n", "except FileNotFoundError:\n", " print(\"Error: The file was not found.\")\n", "except pd.errors.ParserError:\n", " print(\"Error: There was a problem parsing the CSV file.\")\n", "except Exception as e:\n", " print(f\"An unexpected error occurred: {e}\")\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "33v8Kr5LeNYP", "outputId": "2d8e6eef-a25e-4c31-deab-80d4d6da35b8" }, "execution_count": 18, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " ID Name Age Gender City Salary\n", "0 1 Alice 25 Female New York 70000\n", "1 2 Bob 30 Male Los Angeles 80000\n", "2 3 Charlie 35 Male Chicago 90000\n", "3 4 Diana 28 Female Houston 75000\n", "4 5 Edward 40 Male San Francisco 100000\n", "5 6 Faith 32 Female Miami 82000\n", "6 7 George 45 Male Seattle 110000\n", "7 8 Hannah 29 Female Boston 72000\n" ] } ] }, { "cell_type": "code", "source": [ "import csv\n", "\n", "# Path to your CSV file\n", "csv_file_path = 'employee_data.csv'\n", "\n", "# Function to read CSV using csv module\n", "def read_csv_using_csv_module():\n", " try:\n", " with open(r\"/content/sample_data (1).csv\") as file:\n", " csv_reader = csv.DictReader(file) # Use DictReader to access columns by name\n", " print(\"Reading using csv module:\")\n", " for row in csv_reader:\n", " try:\n", " # Ensuring data types are correct (Age should be an integer, Salary should be numeric)\n", " row['Age'] = int(row['Age']) # Convert Age to integer\n", " row['Salary'] = float(row['Salary']) # Convert Salary to float\n", " print(row)\n", " except ValueError as ve:\n", " print(f\"Error: Invalid data type in row {row}. Error: {ve}\")\n", " except FileNotFoundError:\n", " print(\"Error: The file was not found.\")\n", " except csv.Error as e:\n", " print(f\"Error reading the CSV file with csv module: {e}\")\n", " except Exception as e:\n", " print(f\"An unexpected error occurred with csv module: {e}\")\n", "\n", "# Call the function to read using csv module\n", "read_csv_using_csv_module()\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xvRO2NmZkCPJ", "outputId": "abdb3575-5522-42d2-96d2-69231b60b6a7" }, "execution_count": 20, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Reading using csv module:\n", "{'ID': '1', 'Name': 'Alice', 'Age': 25, 'Gender': 'Female', 'City': 'New York', 'Salary': 70000.0}\n", "{'ID': '2', 'Name': 'Bob', 'Age': 30, 'Gender': 'Male', 'City': 'Los Angeles', 'Salary': 80000.0}\n", "{'ID': '3', 'Name': 'Charlie', 'Age': 35, 'Gender': 'Male', 'City': 'Chicago', 'Salary': 90000.0}\n", "{'ID': '4', 'Name': 'Diana', 'Age': 28, 'Gender': 'Female', 'City': 'Houston', 'Salary': 75000.0}\n", "{'ID': '5', 'Name': 'Edward', 'Age': 40, 'Gender': 'Male', 'City': 'San Francisco', 'Salary': 100000.0}\n", "{'ID': '6', 'Name': 'Faith', 'Age': 32, 'Gender': 'Female', 'City': 'Miami', 'Salary': 82000.0}\n", "{'ID': '7', 'Name': 'George', 'Age': 45, 'Gender': 'Male', 'City': 'Seattle', 'Salary': 110000.0}\n", "{'ID': '8', 'Name': 'Hannah', 'Age': 29, 'Gender': 'Female', 'City': 'Boston', 'Salary': 72000.0}\n" ] } ] }, { "cell_type": "code", "source": [ "# Using pandas for Reading and Handling Errors" ], "metadata": { "id": "oXkmSf5lkjiP" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "\n", "# Path to your CSV file\n", "csv_file_path = r\"/content/sample_data (1).csv\"\n", "\n", "# Function to read CSV using pandas\n", "def read_csv_using_pandas():\n", " try:\n", " # Read the CSV file into a pandas DataFrame\n", " df = pd.read_csv(r\"/content/sample_data (1).csv\")\n", " print(\"\\nReading using pandas:\")\n", "\n", " # Ensure that the 'Age' and 'Salary' columns are correctly typed\n", " df['Age'] = pd.to_numeric(df['Age'], errors='raise') # Ensures Age is numeric\n", " df['Salary'] = pd.to_numeric(df['Salary'], errors='raise') # Ensures Salary is numeric\n", "\n", " # Display the DataFrame\n", " print(df)\n", "\n", " except FileNotFoundError:\n", " print(\"Error: The file was not found.\")\n", " except pd.errors.ParserError:\n", " print(\"Error: There was a problem parsing the CSV file with pandas.\")\n", " except ValueError as ve:\n", " print(f\"Error: Invalid data type in the CSV file. {ve}\")\n", " except Exception as e:\n", " print(f\"An unexpected error occurred with pandas: {e}\")\n", "\n", "# Call the function to read using pandas\n", "read_csv_using_pandas()\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "SItXSSqbkpKr", "outputId": "9e6c4168-bc01-49d8-eabd-18dacab7e3c9" }, "execution_count": 21, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "Reading using pandas:\n", " ID Name Age Gender City Salary\n", "0 1 Alice 25 Female New York 70000\n", "1 2 Bob 30 Male Los Angeles 80000\n", "2 3 Charlie 35 Male Chicago 90000\n", "3 4 Diana 28 Female Houston 75000\n", "4 5 Edward 40 Male San Francisco 100000\n", "5 6 Faith 32 Female Miami 82000\n", "6 7 George 45 Male Seattle 110000\n", "7 8 Hannah 29 Female Boston 72000\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "L0TGXiLsk0yi" }, "execution_count": null, "outputs": [] } ] }