{"id":1870,"date":"2025-08-03T03:01:09","date_gmt":"2025-08-03T03:01:09","guid":{"rendered":"https:\/\/itxperts.co.in\/blog\/?p=1870"},"modified":"2025-08-03T03:01:11","modified_gmt":"2025-08-03T03:01:11","slug":"how-to-work-with-csv-json-datasets-python","status":"publish","type":"post","link":"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/","title":{"rendered":"How to Work with CSV and JSON Datasets in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>When working with Machine Learning or data analysis projects, CSV and JSON are the most commonly used file formats for datasets. Python makes it easy to load, parse, and manipulate these formats using built-in libraries and external packages like <code>pandas<\/code> and <code>json<\/code>. In this post, you\u2019ll learn how to work with CSV and JSON data efficiently.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">What is a CSV File?<\/h3>\n\n\n\n<p>CSV (Comma-Separated Values) is a simple text file format used to store tabular data like spreadsheets. Each line represents a row, and commas separate the columns.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Name,Age,Gender\nAlice,30,Female\nBob,25,Male\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">What is a JSON File?<\/h3>\n\n\n\n<p>JSON (JavaScript Object Notation) is a format used to store and transport structured data using key-value pairs. It&#8217;s commonly used in APIs and web data.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"Name\": \"Alice\",\n  \"Age\": 30,\n  \"Gender\": \"Female\"\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Reading CSV Files Using Pandas<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\n\n# Load CSV file\ndf = pd.read_csv('data.csv')\n\n# Display first 5 rows\nprint(df.head())\n<\/code><\/pre>\n\n\n\n<p>You can also specify:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>delimiter<\/code>: if the separator is not a comma.<\/li>\n\n\n\n<li><code>usecols<\/code>: to load specific columns.<\/li>\n\n\n\n<li><code>na_values<\/code>: to handle missing values.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Writing to a CSV File<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>df.to_csv('output.csv', index=False)\n<\/code><\/pre>\n\n\n\n<p>Setting <code>index=False<\/code> ensures the index column is not saved in the file.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Reading JSON Files<\/h3>\n\n\n\n<p>Python provides a built-in <code>json<\/code> module.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\n# Load JSON file\nwith open('data.json', 'r') as file:\n    data = json.load(file)\n\nprint(data)\n<\/code><\/pre>\n\n\n\n<p>For a JSON dataset structured as a list of dictionaries, you can load it into a DataFrame:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df = pd.DataFrame(data)\nprint(df.head())\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Writing JSON Files<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>with open('output.json', 'w') as file:\n    json.dump(data, file, indent=4)\n<\/code><\/pre>\n\n\n\n<p>The <code>indent=4<\/code> makes the output more readable.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using Pandas to Read JSON<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>df = pd.read_json('data.json')\nprint(df.head())\n<\/code><\/pre>\n\n\n\n<p>You can also convert a DataFrame to JSON:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df.to_json('output.json', orient='records', lines=True)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Key Differences Between CSV and JSON<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>CSV<\/th><th>JSON<\/th><\/tr><\/thead><tbody><tr><td>Structure<\/td><td>Tabular<\/td><td>Hierarchical<\/td><\/tr><tr><td>Readability<\/td><td>Human-readable<\/td><td>Human-readable<\/td><\/tr><tr><td>Best for<\/td><td>Tables\/Spreadsheets<\/td><td>Nested or complex data<\/td><\/tr><tr><td>Size<\/td><td>Smaller<\/td><td>Slightly larger<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Understanding how to handle CSV and JSON datasets is essential for any data-driven project. Python\u2019s <code>pandas<\/code> and <code>json<\/code> modules make it simple to read, write, and manipulate data stored in these formats. Mastering this skill will help you handle real-world data more effectively in your Machine Learning journey.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Introduction When working with Machine Learning or data analysis projects, CSV and JSON are the most commonly used file formats for datasets. Python makes it easy to load, parse, and manipulate these formats using built-in libraries and external packages like pandas and json. In this post, you\u2019ll learn how to work with CSV and JSON [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"googlesitekit_rrm_CAow44u0DA:productID":"","footnotes":""},"categories":[216],"tags":[],"class_list":["post-1870","post","type-post","status-publish","format-standard","hentry","category-beginner-level-foundational"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Work with CSV and JSON Datasets in Python - Itxperts<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Work with CSV and JSON Datasets in Python - Itxperts\" \/>\n<meta property=\"og:description\" content=\"Introduction When working with Machine Learning or data analysis projects, CSV and JSON are the most commonly used file formats for datasets. Python makes it easy to load, parse, and manipulate these formats using built-in libraries and external packages like pandas and json. In this post, you\u2019ll learn how to work with CSV and JSON [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Itxperts\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/itxperts.co.in\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-03T03:01:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-03T03:01:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itxperts.co.in\/blog\/wp-content\/uploads\/2025\/05\/cropped-cropped-itxperts_logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"436\" \/>\n\t<meta property=\"og:image:height\" content=\"398\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"@mritxperts\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"@mritxperts\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/\"},\"author\":{\"name\":\"@mritxperts\",\"@id\":\"https:\/\/itxperts.co.in\/blog\/#\/schema\/person\/77ad4d47f9f82583ee23e37010a52fc6\"},\"headline\":\"How to Work with CSV and JSON Datasets in Python\",\"datePublished\":\"2025-08-03T03:01:09+00:00\",\"dateModified\":\"2025-08-03T03:01:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/\"},\"wordCount\":292,\"publisher\":{\"@id\":\"https:\/\/itxperts.co.in\/blog\/#organization\"},\"articleSection\":[\"Beginner Level (Foundational)\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/\",\"url\":\"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/\",\"name\":\"How to Work with CSV and JSON Datasets in Python - Itxperts\",\"isPartOf\":{\"@id\":\"https:\/\/itxperts.co.in\/blog\/#website\"},\"datePublished\":\"2025-08-03T03:01:09+00:00\",\"dateModified\":\"2025-08-03T03:01:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/itxperts.co.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Work with CSV and JSON Datasets in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/itxperts.co.in\/blog\/#website\",\"url\":\"https:\/\/itxperts.co.in\/blog\/\",\"name\":\"Itxperts\",\"description\":\"Leading Website Design Company in Madhya Pradesh\",\"publisher\":{\"@id\":\"https:\/\/itxperts.co.in\/blog\/#organization\"},\"alternateName\":\"Itxperts | Website Development in Madhya Pradesh\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/itxperts.co.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/itxperts.co.in\/blog\/#organization\",\"name\":\"Itxperts\",\"alternateName\":\"Leading Website Design Company in Madhya Pradesh \u2013 Itxperts\",\"url\":\"https:\/\/itxperts.co.in\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/itxperts.co.in\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/itxperts.co.in\/blog\/wp-content\/uploads\/2025\/05\/cropped-itxperts_logo.png\",\"contentUrl\":\"https:\/\/itxperts.co.in\/blog\/wp-content\/uploads\/2025\/05\/cropped-itxperts_logo.png\",\"width\":512,\"height\":512,\"caption\":\"Itxperts\"},\"image\":{\"@id\":\"https:\/\/itxperts.co.in\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/itxperts.co.in\",\"https:\/\/www.linkedin.com\/company\/itxpertsshivpuri\/\",\"https:\/\/www.instagram.com\/itxperts.co.in\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/itxperts.co.in\/blog\/#\/schema\/person\/77ad4d47f9f82583ee23e37010a52fc6\",\"name\":\"@mritxperts\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/itxperts.co.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/702cffafd84d85872c0d42d33a9fa39140418d7c60a1311a1f8f55b005d0570b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/702cffafd84d85872c0d42d33a9fa39140418d7c60a1311a1f8f55b005d0570b?s=96&d=mm&r=g\",\"caption\":\"@mritxperts\"},\"description\":\"I am a full-stack web developer from India with over 8 years of experience in building dynamic and responsive web solutions. Specializing in both front-end and back-end development, I have a passion for creating seamless digital experiences. When I'm not coding, I enjoy sharing insights and tutorials on the latest web technologies, helping fellow developers stay ahead in the ever-evolving tech landscape.\",\"sameAs\":[\"https:\/\/itxperts.co.in\/blog\"],\"url\":\"https:\/\/itxperts.co.in\/blog\/author\/mritxpertsgmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Work with CSV and JSON Datasets in Python - Itxperts","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Work with CSV and JSON Datasets in Python - Itxperts","og_description":"Introduction When working with Machine Learning or data analysis projects, CSV and JSON are the most commonly used file formats for datasets. Python makes it easy to load, parse, and manipulate these formats using built-in libraries and external packages like pandas and json. In this post, you\u2019ll learn how to work with CSV and JSON [&hellip;]","og_url":"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/","og_site_name":"Itxperts","article_publisher":"https:\/\/www.facebook.com\/itxperts.co.in","article_published_time":"2025-08-03T03:01:09+00:00","article_modified_time":"2025-08-03T03:01:11+00:00","og_image":[{"width":436,"height":398,"url":"https:\/\/itxperts.co.in\/blog\/wp-content\/uploads\/2025\/05\/cropped-cropped-itxperts_logo.png","type":"image\/png"}],"author":"@mritxperts","twitter_card":"summary_large_image","twitter_misc":{"Written by":"@mritxperts","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/#article","isPartOf":{"@id":"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/"},"author":{"name":"@mritxperts","@id":"https:\/\/itxperts.co.in\/blog\/#\/schema\/person\/77ad4d47f9f82583ee23e37010a52fc6"},"headline":"How to Work with CSV and JSON Datasets in Python","datePublished":"2025-08-03T03:01:09+00:00","dateModified":"2025-08-03T03:01:11+00:00","mainEntityOfPage":{"@id":"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/"},"wordCount":292,"publisher":{"@id":"https:\/\/itxperts.co.in\/blog\/#organization"},"articleSection":["Beginner Level (Foundational)"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/","url":"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/","name":"How to Work with CSV and JSON Datasets in Python - Itxperts","isPartOf":{"@id":"https:\/\/itxperts.co.in\/blog\/#website"},"datePublished":"2025-08-03T03:01:09+00:00","dateModified":"2025-08-03T03:01:11+00:00","breadcrumb":{"@id":"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/itxperts.co.in\/blog\/how-to-work-with-csv-json-datasets-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/itxperts.co.in\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Work with CSV and JSON Datasets in Python"}]},{"@type":"WebSite","@id":"https:\/\/itxperts.co.in\/blog\/#website","url":"https:\/\/itxperts.co.in\/blog\/","name":"Itxperts","description":"Leading Website Design Company in Madhya Pradesh","publisher":{"@id":"https:\/\/itxperts.co.in\/blog\/#organization"},"alternateName":"Itxperts | Website Development in Madhya Pradesh","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itxperts.co.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/itxperts.co.in\/blog\/#organization","name":"Itxperts","alternateName":"Leading Website Design Company in Madhya Pradesh \u2013 Itxperts","url":"https:\/\/itxperts.co.in\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/itxperts.co.in\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itxperts.co.in\/blog\/wp-content\/uploads\/2025\/05\/cropped-itxperts_logo.png","contentUrl":"https:\/\/itxperts.co.in\/blog\/wp-content\/uploads\/2025\/05\/cropped-itxperts_logo.png","width":512,"height":512,"caption":"Itxperts"},"image":{"@id":"https:\/\/itxperts.co.in\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/itxperts.co.in","https:\/\/www.linkedin.com\/company\/itxpertsshivpuri\/","https:\/\/www.instagram.com\/itxperts.co.in\/"]},{"@type":"Person","@id":"https:\/\/itxperts.co.in\/blog\/#\/schema\/person\/77ad4d47f9f82583ee23e37010a52fc6","name":"@mritxperts","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/itxperts.co.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/702cffafd84d85872c0d42d33a9fa39140418d7c60a1311a1f8f55b005d0570b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/702cffafd84d85872c0d42d33a9fa39140418d7c60a1311a1f8f55b005d0570b?s=96&d=mm&r=g","caption":"@mritxperts"},"description":"I am a full-stack web developer from India with over 8 years of experience in building dynamic and responsive web solutions. Specializing in both front-end and back-end development, I have a passion for creating seamless digital experiences. When I'm not coding, I enjoy sharing insights and tutorials on the latest web technologies, helping fellow developers stay ahead in the ever-evolving tech landscape.","sameAs":["https:\/\/itxperts.co.in\/blog"],"url":"https:\/\/itxperts.co.in\/blog\/author\/mritxpertsgmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/itxperts.co.in\/blog\/wp-json\/wp\/v2\/posts\/1870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itxperts.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itxperts.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itxperts.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itxperts.co.in\/blog\/wp-json\/wp\/v2\/comments?post=1870"}],"version-history":[{"count":1,"href":"https:\/\/itxperts.co.in\/blog\/wp-json\/wp\/v2\/posts\/1870\/revisions"}],"predecessor-version":[{"id":1871,"href":"https:\/\/itxperts.co.in\/blog\/wp-json\/wp\/v2\/posts\/1870\/revisions\/1871"}],"wp:attachment":[{"href":"https:\/\/itxperts.co.in\/blog\/wp-json\/wp\/v2\/media?parent=1870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itxperts.co.in\/blog\/wp-json\/wp\/v2\/categories?post=1870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itxperts.co.in\/blog\/wp-json\/wp\/v2\/tags?post=1870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}