Hello I need some help with this question. Task: Write Python code to take such a record of any length (the data is a sample), and output a table/dataframe comprising a row for each month with columns...

1 answer below »
Hello I need some help with this question. Task: Write Python code to take such a record of any length (the data is a sample), and output a table/dataframe comprising a row for each month with columns for date, start balance, and return. Print out this table/dataframe and then visualize the record as two vertically arranged plots.
Answered Same DayApr 16, 2021

Answer To: Hello I need some help with this question. Task: Write Python code to take such a record of any...

Vibhav answered on Apr 16 2021
142 Votes
bitcoin/bitcoin_return.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Bitcoin\n",
"\n",
"Consider a record of a one-time investment in bitcoin with value of that investment tracked monthly, provided as an (ordered) tuple of dictionaries, where each dictionary comprises one key for the month and corresponding one value for the value of the investment, and the first entry (Jan 2018) is the initial investment made on 01 Jan 2018, shown in `data` below.\n",
"\n",
"Write Python code to take such a record of any length (the below data is only a sample), and output a table/dataframe comprising a row for each month with columns for date, start balance, and return. **Print out this table/dataframe.**\n",
"\n",
"Also, visualize the record as two vertically arranged plots. \n",
"- The top plot should show a line plot of start balance vs. month\n",
"- The bottom plot should show a bar plot of return vs. month, with a black horizontal line at return=0, and bars color-coded such that positive returns are green and negative returns are red. \n",
"- The two plots' horizontal axes should align. Demonstrate that your code works by applying it to `data`.\n",
"\n",
"Notes:
\n",
"- The gain for each period is the end balance minus the start balance. \n",
"- The growth factor for each period is the end balance divided by the start balance. \n",
"- The return for each period is the growth factor minus 1."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"({'Jan 2018': 1000},\n",
" {'Feb 2018': 1100},\n",
" {'Mar 2018': 1400},\n",
" {'Apr 2018': 700},\n",
" {'May 2018': 800},\n",
" {'Jun 2018': 500})"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Do not alter data\n",
"data = ({\"Jan 2018\":1000},{\"Feb 2018\":1100},{\"Mar 2018\":1400},{\"Apr 2018\":700},{\"May 2018\":800},{\"Jun 2018\":500})\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"d = dict()\n",
"months = []\n",
"values = []\n",
"for t in data:\n",
" m, v = list(t.items())[0][0], list(t.items())[0][1]\n",
" months.append(m)\n",
" values.append(v)\n",
"\n",
"d[\"month\"] = months\n",
"d[\"value\"] = values\n",
"df = pd.DataFrame(d)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"
monthvalue
0Jan 20181000
1Feb 20181100
2Mar 20181400
3Apr 2018700
4May 2018800
5Jun 2018500
\n",
"
"
],
"text/plain": [
" month value\n",
"0 Jan 2018 1000\n",
"1 Feb 2018 1100\n",
"2 Mar 2018 1400\n",
"3 Apr 2018 700\n",
"4 May 2018 800\n",
"5 Jun 2018 500"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"df.insert(2, 'start', 0)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"
monthvaluestart
0Jan 201810000
1Feb 201811000
2Mar 201814000
3Apr 20187000
4May 20188000
5Jun 20185000
\n",
"
"
],
"text/plain": [
" month value start\n",
"0 Jan 2018 1000 0\n",
"1 Feb 2018 1100 0\n",
"2 Mar 2018 1400 0\n",
"3 Apr 2018 700 0\n",
"4 May 2018 800 0\n",
"5 Jun 2018 500 0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"rows = len(df.index)\n",
"df.loc[0, 'start'] = df.loc[0, 'value']\n",
"for i in range(1, rows):\n",
" df.loc[i, 'start'] = df.loc[i-1, 'value']"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
" ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here
monthvaluestart
0Jan 201810001000
1Feb 201811001000
2Mar 201814001100
3Apr 2018