{"id":4299,"date":"2024-08-21T19:46:02","date_gmt":"2024-08-22T03:46:02","guid":{"rendered":"https:\/\/crm270s.abramjmeister.com\/?p=4299"},"modified":"2024-08-21T19:46:02","modified_gmt":"2024-08-22T03:46:02","slug":"python-code-in-jupyter-summing-each-item-each-month-of-%ec%a7%80%ec%b6%9c-%ec%95%a1%ec%88%98-csv-in-sqlite","status":"publish","type":"post","link":"https:\/\/crm270s.mycpamytax.com\/?p=4299","title":{"rendered":"Python Code in Jupyter summing each item, each month of \uc9c0\ucd9c-\uc561\uc218.csv in SQLite"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import sqlite3\nimport pandas as pd\n\n# Define the path to your SQLite database\ndb_path = \"\/home\/jbyungrokim\/CSV\/CSV.db\"\n\n# Connect to the SQLite database\nconn = sqlite3.connect(db_path)\n\n# Adjust the query to calculate the sum for each item by year and month\nquery = \"\"\"\nSELECT \n    strftime('%Y-%m', '20' || substr(Date_Transaction, 7, 2) || '-' || substr(Date_Transaction, 1, 2) || '-' || substr(Date_Transaction, 4, 2)) AS YearMonth,\n    Item, \n    SUM(Pay_Amount) AS Total_Spent\nFROM \n    '\uc9c0\ucd9c-\uc561\uc218_2024_08_20'\nGROUP BY \n    YearMonth, Item\nORDER BY \n    YearMonth, Total_Spent DESC;\n\"\"\"\n\n# Execute the query and store the result in a pandas DataFrame\nmonthly_sum = pd.read_sql_query(query, conn)\n\n# Reshape the data into a wide format for better readability in a spreadsheet\nmonthly_sum_wide = monthly_sum.pivot(index='Item', columns='YearMonth', values='Total_Spent').fillna(0)\n\n# Export the data to a CSV file\noutput_path = \"\/home\/jbyungrokim\/CSV\/monthly_sum_by_item_Jupyter.csv\"\nmonthly_sum_wide.to_csv(output_path, index=True)\n\n# Print the result\nprint(\"Sum of Pay_Amount by Item for each Year\/Month:\")\nprint(monthly_sum_wide)\n\n# Close the connection to the SQLite database\nconn.close()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[26],"tags":[],"class_list":["post-4299","post","type-post","status-publish","format-standard","hentry","category-codes"],"_links":{"self":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/4299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4299"}],"version-history":[{"count":0,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=\/wp\/v2\/posts\/4299\/revisions"}],"wp:attachment":[{"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crm270s.mycpamytax.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}