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
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": []
|
7 |
+
},
|
8 |
+
"kernelspec": {
|
9 |
+
"name": "python3",
|
10 |
+
"display_name": "Python 3"
|
11 |
+
},
|
12 |
+
"language_info": {
|
13 |
+
"name": "python"
|
14 |
+
}
|
15 |
+
},
|
16 |
+
"cells": [
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": null,
|
20 |
+
"metadata": {
|
21 |
+
"id": "0r5hCx60Yv0u"
|
22 |
+
},
|
23 |
+
"outputs": [],
|
24 |
+
"source": [
|
25 |
+
"import pandas as pd\n"
|
26 |
+
]
|
27 |
+
},
|
28 |
+
{
|
29 |
+
"cell_type": "code",
|
30 |
+
"source": [
|
31 |
+
"#2. Reading a CSV File"
|
32 |
+
],
|
33 |
+
"metadata": {
|
34 |
+
"id": "dytrA9JLZJaO"
|
35 |
+
},
|
36 |
+
"execution_count": null,
|
37 |
+
"outputs": []
|
38 |
+
},
|
39 |
+
{
|
40 |
+
"cell_type": "code",
|
41 |
+
"source": [
|
42 |
+
"import pandas as pd\n",
|
43 |
+
"\n",
|
44 |
+
"try:\n",
|
45 |
+
" # Read the CSV file into a DataFrame\n",
|
46 |
+
" df = pd.read_csv(r\"/content/sample_data (1).csv\")\n",
|
47 |
+
" print(df)\n",
|
48 |
+
"except FileNotFoundError:\n",
|
49 |
+
" print(\"Error: The file was not found.\")\n",
|
50 |
+
"except pd.errors.ParserError:\n",
|
51 |
+
" print(\"Error: There was a problem parsing the CSV file.\")\n",
|
52 |
+
"except Exception as e:\n",
|
53 |
+
" print(f\"An unexpected error occurred: {e}\")\n"
|
54 |
+
],
|
55 |
+
"metadata": {
|
56 |
+
"colab": {
|
57 |
+
"base_uri": "https://localhost:8080/"
|
58 |
+
},
|
59 |
+
"id": "33v8Kr5LeNYP",
|
60 |
+
"outputId": "2d8e6eef-a25e-4c31-deab-80d4d6da35b8"
|
61 |
+
},
|
62 |
+
"execution_count": 18,
|
63 |
+
"outputs": [
|
64 |
+
{
|
65 |
+
"output_type": "stream",
|
66 |
+
"name": "stdout",
|
67 |
+
"text": [
|
68 |
+
" ID Name Age Gender City Salary\n",
|
69 |
+
"0 1 Alice 25 Female New York 70000\n",
|
70 |
+
"1 2 Bob 30 Male Los Angeles 80000\n",
|
71 |
+
"2 3 Charlie 35 Male Chicago 90000\n",
|
72 |
+
"3 4 Diana 28 Female Houston 75000\n",
|
73 |
+
"4 5 Edward 40 Male San Francisco 100000\n",
|
74 |
+
"5 6 Faith 32 Female Miami 82000\n",
|
75 |
+
"6 7 George 45 Male Seattle 110000\n",
|
76 |
+
"7 8 Hannah 29 Female Boston 72000\n"
|
77 |
+
]
|
78 |
+
}
|
79 |
+
]
|
80 |
+
},
|
81 |
+
{
|
82 |
+
"cell_type": "code",
|
83 |
+
"source": [
|
84 |
+
"import csv\n",
|
85 |
+
"\n",
|
86 |
+
"# Path to your CSV file\n",
|
87 |
+
"csv_file_path = 'employee_data.csv'\n",
|
88 |
+
"\n",
|
89 |
+
"# Function to read CSV using csv module\n",
|
90 |
+
"def read_csv_using_csv_module():\n",
|
91 |
+
" try:\n",
|
92 |
+
" with open(r\"/content/sample_data (1).csv\") as file:\n",
|
93 |
+
" csv_reader = csv.DictReader(file) # Use DictReader to access columns by name\n",
|
94 |
+
" print(\"Reading using csv module:\")\n",
|
95 |
+
" for row in csv_reader:\n",
|
96 |
+
" try:\n",
|
97 |
+
" # Ensuring data types are correct (Age should be an integer, Salary should be numeric)\n",
|
98 |
+
" row['Age'] = int(row['Age']) # Convert Age to integer\n",
|
99 |
+
" row['Salary'] = float(row['Salary']) # Convert Salary to float\n",
|
100 |
+
" print(row)\n",
|
101 |
+
" except ValueError as ve:\n",
|
102 |
+
" print(f\"Error: Invalid data type in row {row}. Error: {ve}\")\n",
|
103 |
+
" except FileNotFoundError:\n",
|
104 |
+
" print(\"Error: The file was not found.\")\n",
|
105 |
+
" except csv.Error as e:\n",
|
106 |
+
" print(f\"Error reading the CSV file with csv module: {e}\")\n",
|
107 |
+
" except Exception as e:\n",
|
108 |
+
" print(f\"An unexpected error occurred with csv module: {e}\")\n",
|
109 |
+
"\n",
|
110 |
+
"# Call the function to read using csv module\n",
|
111 |
+
"read_csv_using_csv_module()\n"
|
112 |
+
],
|
113 |
+
"metadata": {
|
114 |
+
"colab": {
|
115 |
+
"base_uri": "https://localhost:8080/"
|
116 |
+
},
|
117 |
+
"id": "xvRO2NmZkCPJ",
|
118 |
+
"outputId": "abdb3575-5522-42d2-96d2-69231b60b6a7"
|
119 |
+
},
|
120 |
+
"execution_count": 20,
|
121 |
+
"outputs": [
|
122 |
+
{
|
123 |
+
"output_type": "stream",
|
124 |
+
"name": "stdout",
|
125 |
+
"text": [
|
126 |
+
"Reading using csv module:\n",
|
127 |
+
"{'ID': '1', 'Name': 'Alice', 'Age': 25, 'Gender': 'Female', 'City': 'New York', 'Salary': 70000.0}\n",
|
128 |
+
"{'ID': '2', 'Name': 'Bob', 'Age': 30, 'Gender': 'Male', 'City': 'Los Angeles', 'Salary': 80000.0}\n",
|
129 |
+
"{'ID': '3', 'Name': 'Charlie', 'Age': 35, 'Gender': 'Male', 'City': 'Chicago', 'Salary': 90000.0}\n",
|
130 |
+
"{'ID': '4', 'Name': 'Diana', 'Age': 28, 'Gender': 'Female', 'City': 'Houston', 'Salary': 75000.0}\n",
|
131 |
+
"{'ID': '5', 'Name': 'Edward', 'Age': 40, 'Gender': 'Male', 'City': 'San Francisco', 'Salary': 100000.0}\n",
|
132 |
+
"{'ID': '6', 'Name': 'Faith', 'Age': 32, 'Gender': 'Female', 'City': 'Miami', 'Salary': 82000.0}\n",
|
133 |
+
"{'ID': '7', 'Name': 'George', 'Age': 45, 'Gender': 'Male', 'City': 'Seattle', 'Salary': 110000.0}\n",
|
134 |
+
"{'ID': '8', 'Name': 'Hannah', 'Age': 29, 'Gender': 'Female', 'City': 'Boston', 'Salary': 72000.0}\n"
|
135 |
+
]
|
136 |
+
}
|
137 |
+
]
|
138 |
+
},
|
139 |
+
{
|
140 |
+
"cell_type": "code",
|
141 |
+
"source": [
|
142 |
+
"# Using pandas for Reading and Handling Errors"
|
143 |
+
],
|
144 |
+
"metadata": {
|
145 |
+
"id": "oXkmSf5lkjiP"
|
146 |
+
},
|
147 |
+
"execution_count": null,
|
148 |
+
"outputs": []
|
149 |
+
},
|
150 |
+
{
|
151 |
+
"cell_type": "code",
|
152 |
+
"source": [
|
153 |
+
"import pandas as pd\n",
|
154 |
+
"\n",
|
155 |
+
"# Path to your CSV file\n",
|
156 |
+
"csv_file_path = r\"/content/sample_data (1).csv\"\n",
|
157 |
+
"\n",
|
158 |
+
"# Function to read CSV using pandas\n",
|
159 |
+
"def read_csv_using_pandas():\n",
|
160 |
+
" try:\n",
|
161 |
+
" # Read the CSV file into a pandas DataFrame\n",
|
162 |
+
" df = pd.read_csv(r\"/content/sample_data (1).csv\")\n",
|
163 |
+
" print(\"\\nReading using pandas:\")\n",
|
164 |
+
"\n",
|
165 |
+
" # Ensure that the 'Age' and 'Salary' columns are correctly typed\n",
|
166 |
+
" df['Age'] = pd.to_numeric(df['Age'], errors='raise') # Ensures Age is numeric\n",
|
167 |
+
" df['Salary'] = pd.to_numeric(df['Salary'], errors='raise') # Ensures Salary is numeric\n",
|
168 |
+
"\n",
|
169 |
+
" # Display the DataFrame\n",
|
170 |
+
" print(df)\n",
|
171 |
+
"\n",
|
172 |
+
" except FileNotFoundError:\n",
|
173 |
+
" print(\"Error: The file was not found.\")\n",
|
174 |
+
" except pd.errors.ParserError:\n",
|
175 |
+
" print(\"Error: There was a problem parsing the CSV file with pandas.\")\n",
|
176 |
+
" except ValueError as ve:\n",
|
177 |
+
" print(f\"Error: Invalid data type in the CSV file. {ve}\")\n",
|
178 |
+
" except Exception as e:\n",
|
179 |
+
" print(f\"An unexpected error occurred with pandas: {e}\")\n",
|
180 |
+
"\n",
|
181 |
+
"# Call the function to read using pandas\n",
|
182 |
+
"read_csv_using_pandas()\n"
|
183 |
+
],
|
184 |
+
"metadata": {
|
185 |
+
"colab": {
|
186 |
+
"base_uri": "https://localhost:8080/"
|
187 |
+
},
|
188 |
+
"id": "SItXSSqbkpKr",
|
189 |
+
"outputId": "9e6c4168-bc01-49d8-eabd-18dacab7e3c9"
|
190 |
+
},
|
191 |
+
"execution_count": 21,
|
192 |
+
"outputs": [
|
193 |
+
{
|
194 |
+
"output_type": "stream",
|
195 |
+
"name": "stdout",
|
196 |
+
"text": [
|
197 |
+
"\n",
|
198 |
+
"Reading using pandas:\n",
|
199 |
+
" ID Name Age Gender City Salary\n",
|
200 |
+
"0 1 Alice 25 Female New York 70000\n",
|
201 |
+
"1 2 Bob 30 Male Los Angeles 80000\n",
|
202 |
+
"2 3 Charlie 35 Male Chicago 90000\n",
|
203 |
+
"3 4 Diana 28 Female Houston 75000\n",
|
204 |
+
"4 5 Edward 40 Male San Francisco 100000\n",
|
205 |
+
"5 6 Faith 32 Female Miami 82000\n",
|
206 |
+
"6 7 George 45 Male Seattle 110000\n",
|
207 |
+
"7 8 Hannah 29 Female Boston 72000\n"
|
208 |
+
]
|
209 |
+
}
|
210 |
+
]
|
211 |
+
},
|
212 |
+
{
|
213 |
+
"cell_type": "code",
|
214 |
+
"source": [],
|
215 |
+
"metadata": {
|
216 |
+
"id": "L0TGXiLsk0yi"
|
217 |
+
},
|
218 |
+
"execution_count": null,
|
219 |
+
"outputs": []
|
220 |
+
}
|
221 |
+
]
|
222 |
+
}
|