Skip to content

Instantly share code, notes, and snippets.

@roshantaneja
Last active August 3, 2022 19:53
Show Gist options
  • Save roshantaneja/c099a49cf0cd09f674009e6f6f24bb43 to your computer and use it in GitHub Desktop.
Save roshantaneja/c099a49cf0cd09f674009e6f6f24bb43 to your computer and use it in GitHub Desktop.
CSV-Manipulator.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "CSV-Manipulator.ipynb",
"private_outputs": true,
"provenance": [],
"collapsed_sections": [],
"mount_file_id": "https://gist.github.com/Daroshi11260/c099a49cf0cd09f674009e6f6f24bb43#file-csv-manipulator-ipynb",
"authorship_tag": "ABX9TyPN3JJhzra+LYkWiwH9MEiv",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/Daroshi11260/c099a49cf0cd09f674009e6f6f24bb43/csv-manipulator.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Heres an easy CSV Manipulator for reformatting some CSVs from ourworldindata\n",
"\n",
"heres the issue:\n",
"1. the OurWorldInData CSVs that we download have the country as the independent variable instead of the year which is the only valuble aspect of the CSV as the independent variable.\n",
"\n",
"1. To reformat the CSV we had to create a new one by changing the labels to year-country with the gdp as the value in the table\n",
"\n",
"1. by getting rid of unneccessary information, google sheets is able to understand it to produce tables that fit better in the sheets chart agorithm."
],
"metadata": {
"id": "L1zMPK_A0Kst"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "J8hKV9SdoRlh"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import csv\n",
"\n",
"#open CSV\n",
"file = open('/content/life-expectancy.csv')\n",
"\n",
"type(file)\n",
"\n",
"#read CSV\n",
"csvreader = csv.reader(file)\n",
"\n",
"#extract header names\n",
"header = []\n",
"header = next(csvreader)\n",
"header\n",
"\n",
"#send rows to array\n",
"rows = []\n",
"for row in csvreader:\n",
" rows.append(row)\n",
"rows\n",
"\n",
"print(rows)\n",
"\n",
"file.close()"
]
},
{
"cell_type": "markdown",
"source": [
"#Now we have a list of values in the array rows \n",
"\n",
"---\n",
"\n",
"we are going to delete all rows that dont have the countries of interest in them\n",
"\n",
"`for all in rows, find != (preferred list), pop()`\n"
],
"metadata": {
"id": "YbGohFNP0iMx"
}
},
{
"cell_type": "code",
"source": [
"shortened = []\n",
"\n",
"countries = ['IND', 'GBR', 'DEU', 'JPN', 'USA']\n",
"\n",
"for i in rows:\n",
" for j in countries:\n",
" if i[1] == j:\n",
" shortened.append(i[1:4])\n",
"\n",
"print(shortened)"
],
"metadata": {
"id": "dL_IQciV1GsA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Now we reformat the 2d array so that it fits the format required for the csv"
],
"metadata": {
"id": "ZcYnJtKPo-Sn"
}
},
{
"cell_type": "code",
"source": [
"final = []\n",
"\n",
"with open('students.csv', 'w') as file:\n",
" # Create a CSV dictionary writer and add the student header as field names\n",
" writer = csv.writer(file)\n",
" # Use writerows() not writerow()\n",
" writer.writerows(shortened)"
],
"metadata": {
"id": "xNt53DlpuO9Q"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Now we have to export it to the output csv\n",
"---\n",
"(meaning i have to learn how to write to a file now lmaooo)"
],
"metadata": {
"id": "sGmGQNEsovwB"
}
},
{
"cell_type": "code",
"source": [
"import matplotlib.pyplot as plt\n",
"plt.plot()"
],
"metadata": {
"id": "mWe5ZRlso8sk"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment