{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "collapsed_sections": [], "authorship_tag": "ABX9TyN168UGO01MfLo+pjIzDQD1", "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": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# pandas\n", "\n", "Pandas objects can be thought of as enhanced versions of NumPy structured arrays in which the rows and columns are identified with labels rather than simple integer indices.\n", "\n", "Tree fundamental Pandas data structures: \n", " ```\n", " the Series, \n", " DataFrame, \n", " and Index.\n", "```\n", "\n" ], "metadata": { "id": "c8WKgTLrD2Ny" } }, { "cell_type": "code", "source": [ "import pandas as pd\n", "pd.__version__" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "Ku_XIFYF84Ip", "outputId": "2a83f829-ec02-4348-c3cb-e2a5c509df6c" }, "execution_count": 260, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'1.3.5'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 260 } ] }, { "cell_type": "code", "source": [ "print(help(pd))" ], "metadata": { "id": "yxma0Hea88r8", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "9d8b0cb6-4b61-4f46-94fe-e7a3c016f1d8" }, "execution_count": 261, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Help on package pandas:\n", "\n", "NAME\n", " pandas\n", "\n", "DESCRIPTION\n", " pandas - a powerful data analysis and manipulation library for Python\n", " =====================================================================\n", " \n", " **pandas** is a Python package providing fast, flexible, and expressive data\n", " structures designed to make working with \"relational\" or \"labeled\" data both\n", " easy and intuitive. It aims to be the fundamental high-level building block for\n", " doing practical, **real world** data analysis in Python. Additionally, it has\n", " the broader goal of becoming **the most powerful and flexible open source data\n", " analysis / manipulation tool available in any language**. It is already well on\n", " its way toward this goal.\n", " \n", " Main Features\n", " -------------\n", " Here are just a few of the things that pandas does well:\n", " \n", " - Easy handling of missing data in floating point as well as non-floating\n", " point data.\n", " - Size mutability: columns can be inserted and deleted from DataFrame and\n", " higher dimensional objects\n", " - Automatic and explicit data alignment: objects can be explicitly aligned\n", " to a set of labels, or the user can simply ignore the labels and let\n", " `Series`, `DataFrame`, etc. automatically align the data for you in\n", " computations.\n", " - Powerful, flexible group by functionality to perform split-apply-combine\n", " operations on data sets, for both aggregating and transforming data.\n", " - Make it easy to convert ragged, differently-indexed data in other Python\n", " and NumPy data structures into DataFrame objects.\n", " - Intelligent label-based slicing, fancy indexing, and subsetting of large\n", " data sets.\n", " - Intuitive merging and joining data sets.\n", " - Flexible reshaping and pivoting of data sets.\n", " - Hierarchical labeling of axes (possible to have multiple labels per tick).\n", " - Robust IO tools for loading data from flat files (CSV and delimited),\n", " Excel files, databases, and saving/loading data from the ultrafast HDF5\n", " format.\n", " - Time series-specific functionality: date range generation and frequency\n", " conversion, moving window statistics, date shifting and lagging.\n", "\n", "PACKAGE CONTENTS\n", " _config (package)\n", " _libs (package)\n", " _testing (package)\n", " _typing\n", " _version\n", " api (package)\n", " arrays (package)\n", " compat (package)\n", " conftest\n", " core (package)\n", " errors (package)\n", " io (package)\n", " plotting (package)\n", " testing\n", " tests (package)\n", " tseries (package)\n", " util (package)\n", "\n", "SUBMODULES\n", " _hashtable\n", " _lib\n", " _tslib\n", " offsets\n", "\n", "FUNCTIONS\n", " __getattr__(name)\n", " # GH 27101\n", "\n", "DATA\n", " IndexSlice = \n", " NA = \n", " NaT = NaT\n", " __docformat__ = 'restructuredtext'\n", " __git_version__ = '66e3805b8cabe977f40c05259cc3fcf7ead5687d'\n", " describe_option = \n", " get_option = \n", " options = \n", " reset_option = \n", " set_option = \n", "\n", "VERSION\n", " 1.3.5\n", "\n", "FILE\n", " /usr/local/lib/python3.7/dist-packages/pandas/__init__.py\n", "\n", "\n", "None\n" ] } ] }, { "cell_type": "code", "source": [ "dir(pd)" ], "metadata": { "id": "3O6NyEIv9adB", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "874e205d-ac41-4fad-8d5b-49ef72072314" }, "execution_count": 262, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['BooleanDtype',\n", " 'Categorical',\n", " 'CategoricalDtype',\n", " 'CategoricalIndex',\n", " 'DataFrame',\n", " 'DateOffset',\n", " 'DatetimeIndex',\n", " 'DatetimeTZDtype',\n", " 'ExcelFile',\n", " 'ExcelWriter',\n", " 'Flags',\n", " 'Float32Dtype',\n", " 'Float64Dtype',\n", " 'Float64Index',\n", " 'Grouper',\n", " 'HDFStore',\n", " 'Index',\n", " 'IndexSlice',\n", " 'Int16Dtype',\n", " 'Int32Dtype',\n", " 'Int64Dtype',\n", " 'Int64Index',\n", " 'Int8Dtype',\n", " 'Interval',\n", " 'IntervalDtype',\n", " 'IntervalIndex',\n", " 'MultiIndex',\n", " 'NA',\n", " 'NaT',\n", " 'NamedAgg',\n", " 'Period',\n", " 'PeriodDtype',\n", " 'PeriodIndex',\n", " 'RangeIndex',\n", " 'Series',\n", " 'SparseDtype',\n", " 'StringDtype',\n", " 'Timedelta',\n", " 'TimedeltaIndex',\n", " 'Timestamp',\n", " 'UInt16Dtype',\n", " 'UInt32Dtype',\n", " 'UInt64Dtype',\n", " 'UInt64Index',\n", " 'UInt8Dtype',\n", " '__builtins__',\n", " '__cached__',\n", " '__doc__',\n", " '__docformat__',\n", " '__file__',\n", " '__getattr__',\n", " '__git_version__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__path__',\n", " '__spec__',\n", " '__version__',\n", " '_config',\n", " '_hashtable',\n", " '_is_numpy_dev',\n", " '_lib',\n", " '_libs',\n", " '_np_version_under1p18',\n", " '_testing',\n", " '_tslib',\n", " '_typing',\n", " '_version',\n", " 'api',\n", " 'array',\n", " 'arrays',\n", " 'bdate_range',\n", " 'compat',\n", " 'concat',\n", " 'core',\n", " 'crosstab',\n", " 'cut',\n", " 'date_range',\n", " 'describe_option',\n", " 'errors',\n", " 'eval',\n", " 'factorize',\n", " 'get_dummies',\n", " 'get_option',\n", " 'infer_freq',\n", " 'interval_range',\n", " 'io',\n", " 'isna',\n", " 'isnull',\n", " 'json_normalize',\n", " 'lreshape',\n", " 'melt',\n", " 'merge',\n", " 'merge_asof',\n", " 'merge_ordered',\n", " 'notna',\n", " 'notnull',\n", " 'offsets',\n", " 'option_context',\n", " 'options',\n", " 'pandas',\n", " 'period_range',\n", " 'pivot',\n", " 'pivot_table',\n", " 'plotting',\n", " 'qcut',\n", " 'read_clipboard',\n", " 'read_csv',\n", " 'read_excel',\n", " 'read_feather',\n", " 'read_fwf',\n", " 'read_gbq',\n", " 'read_hdf',\n", " 'read_html',\n", " 'read_json',\n", " 'read_orc',\n", " 'read_parquet',\n", " 'read_pickle',\n", " 'read_sas',\n", " 'read_spss',\n", " 'read_sql',\n", " 'read_sql_query',\n", " 'read_sql_table',\n", " 'read_stata',\n", " 'read_table',\n", " 'read_xml',\n", " 'reset_option',\n", " 'set_eng_float_format',\n", " 'set_option',\n", " 'show_versions',\n", " 'test',\n", " 'testing',\n", " 'timedelta_range',\n", " 'to_datetime',\n", " 'to_numeric',\n", " 'to_pickle',\n", " 'to_timedelta',\n", " 'tseries',\n", " 'unique',\n", " 'util',\n", " 'value_counts',\n", " 'wide_to_long']" ] }, "metadata": {}, "execution_count": 262 } ] }, { "cell_type": "markdown", "metadata": { "id": "KRI3nzsAEjq0" }, "source": [ "### Creating a pandas Series\n", "A Pandas Series is a one-dimensional array of indexed data where data can be many different things:\n", "```\n", " a Python dict\n", " an ndarray\n", " a scalar value (like 5)\n", "```" ] }, { "cell_type": "code", "source": [ "import pandas as pd; import numpy as np" ], "metadata": { "id": "Swf0Wgv2Ow6V" }, "execution_count": 263, "outputs": [] }, { "cell_type": "code", "source": [ "pd.Series([1, 2, 3, 4, 5])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_BPgcB1gEeie", "outputId": "1a031356-e362-4b1f-fcf4-397486688e32" }, "execution_count": 264, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0 1\n", "1 2\n", "2 3\n", "3 4\n", "4 5\n", "dtype: int64" ] }, "metadata": {}, "execution_count": 264 } ] }, { "cell_type": "code", "source": [ "pd.Series(np.random.randn(5), index=[\"a\", \"b\", \"c\", \"d\", \"e\"])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "OPlP3UmxEenI", "outputId": "8bd23701-9a9e-4c78-b0e8-458498ddad38" }, "execution_count": 265, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "a -0.203503\n", "b 1.013045\n", "c 2.181949\n", "d -1.122588\n", "e 1.189388\n", "dtype: float64" ] }, "metadata": {}, "execution_count": 265 } ] }, { "cell_type": "code", "source": [ "pd.Series(np.array([1, 2, 3, 4, 5]))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "fsFizu_gEef7", "outputId": "3ad2f8a0-91bd-450a-bdf0-afd9ac9ecef1" }, "execution_count": 266, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0 1\n", "1 2\n", "2 3\n", "3 4\n", "4 5\n", "dtype: int64" ] }, "metadata": {}, "execution_count": 266 } ] }, { "cell_type": "code", "source": [ "pd.Series(np.array([1, 2, 3, 4, 5])) + pd.Series(np.array([5, 4, 3, 2, 1]))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "7fbf4686-dbba-4f63-c4a3-798e2e94ef6a", "id": "uEi2h7BxISDE" }, "execution_count": 267, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0 6\n", "1 6\n", "2 6\n", "3 6\n", "4 6\n", "dtype: int64" ] }, "metadata": {}, "execution_count": 267 } ] }, { "cell_type": "code", "source": [ "d = {\"b\": 1, \"a\": 0, \"c\": 2}; pd.Series(d)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RPBi27RvEek2", "outputId": "26076598-45f1-4f14-ba38-3b70abfe4414" }, "execution_count": 268, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "b 1\n", "a 0\n", "c 2\n", "dtype: int64" ] }, "metadata": {}, "execution_count": 268 } ] }, { "cell_type": "markdown", "source": [ "The Series object is basically interchangeable with a one-dimensional NumPy array. The essential difference is the presence of the index: while the Numpy Array has an implicitly defined integer index used to access the values, the Pandas Series has an explicitly defined index associated with the values." ], "metadata": { "id": "oDM7MwfvW6BN" } }, { "cell_type": "markdown", "source": [ "### Creating a pandas DataFrame\n", "`DataFrame` is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object. Like Series, DataFrame accepts many different kinds of input:\n", "\n", " Dict of 1D ndarrays, lists, dicts, or Series\n", "\n", " 2-D numpy.ndarray\n", "\n", " Structured or record ndarray\n", "\n", " A Series\n", "\n", " Another DataFrame\n" ], "metadata": { "id": "jFmcFw6EI0WV" } }, { "cell_type": "code", "metadata": { "id": "Jm8B74lFX48n", "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "outputId": "6fa724bf-7b2e-4946-c0c2-4f134de9a46e" }, "source": [ "df = pd.DataFrame(np.random.randn(5,4),index='A B C D E'.split(),columns='W X Y Z'.split())\n", "df" ], "execution_count": 269, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " W X Y Z\n", "A -1.055250 -0.255963 0.523574 -0.216817\n", "B -1.587131 -0.128045 -0.767518 -0.055505\n", "C -0.589701 -1.098669 1.061195 -0.407758\n", "D -0.692038 -0.886125 0.937507 0.980382\n", "E -1.218705 -0.135298 1.196934 1.009017" ], "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", " \n", " \n", " \n", "
WXYZ
A-1.055250-0.2559630.523574-0.216817
B-1.587131-0.128045-0.767518-0.055505
C-0.589701-1.0986691.061195-0.407758
D-0.692038-0.8861250.9375070.980382
E-1.218705-0.1352981.1969341.009017
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 269 } ] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "d = {\"one\": pd.Series([1.0, 2.0, 3.0], index=[\"a\", \"b\", \"c\"]),\n", " \"two\": pd.Series([1.0, 2.0, 3.0, 4.0], index=[\"a\", \"b\", \"c\", \"d\"])\n", " }\n", "df = pd.DataFrame(d); print(type(df)); print('\\n'); df.info(); print('\\n'); df" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 418 }, "id": "KZSGAmbAEeVW", "outputId": "59ac9dae-5b80-4724-b94b-dc2cce5d79cf" }, "execution_count": 270, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "\n", "\n", "\n", "Index: 4 entries, a to d\n", "Data columns (total 2 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 one 3 non-null float64\n", " 1 two 4 non-null float64\n", "dtypes: float64(2)\n", "memory usage: 96.0+ bytes\n", "\n", "\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ " one two\n", "a 1.0 1.0\n", "b 2.0 2.0\n", "c 3.0 3.0\n", "d NaN 4.0" ], "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", "
onetwo
a1.01.0
b2.02.0
c3.03.0
dNaN4.0
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 270 } ] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "df=pd.DataFrame([[1, 2, 3, 4],\n", " [5, 6, 7, 8],\n", " [9, 20, 22, 34]],\n", " index=[0,1,2],\n", " columns=['A', 'B', 'C', 'D'])\n", "df" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "40nKkQcPBNHT", "outputId": "75a5e767-a9b5-430c-c13b-8661e95f13ca" }, "execution_count": 271, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " A B C D\n", "0 1 2 3 4\n", "1 5 6 7 8\n", "2 9 20 22 34" ], "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", "
ABCD
01234
15678
29202234
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 271 } ] }, { "cell_type": "markdown", "source": [ "# Yahoo Finance Data\n", "### creating dataframe" ], "metadata": { "id": "owCVVEuIBrAF" } }, { "cell_type": "code", "source": [ "# source: Yahoo Finance\n", "stock = pd.DataFrame([['tech', 'AAPL', 157.25, 1.23, 26.11, 2539, 0.58, 184.01],\n", " ['tech', 'MSFT', 261.47, 0.93, 26.98, 1942, 0.95, 333.00],\n", " ['engr', 'XOM', 93.87, 1.08, 10.27, 391, 3.68, 103.28],\n", " ['engr', 'COP', 109.45, 1.38, 8.87, 137, 1,75],\n", " ['fin', 'BAC',33.47, 1.40, 10.46, 269, 2.62, 42.41],\n", " ['fin', 'JPM',114.51, 1.10, 9.18, 336, 3.52, 138.80],\n", " ['fin', 'WMT',335.50, 1.39, 7.53, 114, 3.00, 394.07]])\n", "stock" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "jPZIYblhvAQu", "outputId": "643aa04d-4821-4287-a35a-ece429d8dd92" }, "execution_count": 272, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " 0 1 2 3 4 5 6 7\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "2 engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "3 engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "5 fin JPM 114.51 1.10 9.18 336 3.52 138.80\n", "6 fin WMT 335.50 1.39 7.53 114 3.00 394.07" ], "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", " \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", "
01234567
0techAAPL157.251.2326.1125390.58184.01
1techMSFT261.470.9326.9819420.95333.00
2engrXOM93.871.0810.273913.68103.28
3engrCOP109.451.388.871371.0075.00
4finBAC33.471.4010.462692.6242.41
5finJPM114.511.109.183363.52138.80
6finWMT335.501.397.531143.00394.07
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 272 } ] }, { "cell_type": "markdown", "source": [ "### assign column names and index values" ], "metadata": { "id": "hlIbn8EMBzU1" } }, { "cell_type": "code", "source": [ "stock.columns=['sector', 'ticker', 'price', 'beta', 'pe', 'mktcap', 'dy', 'target']\n", "stock.index = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\"]\n", "stock" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "Haz0piap32Av", "outputId": "af636e0a-5fb9-4ab3-ca9c-b35db578cb7e" }, "execution_count": 273, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy target\n", "a tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "b tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "c engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "d engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "e fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "f fin JPM 114.51 1.10 9.18 336 3.52 138.80\n", "g fin WMT 335.50 1.39 7.53 114 3.00 394.07" ], "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", " \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", "
sectortickerpricebetapemktcapdytarget
atechAAPL157.251.2326.1125390.58184.01
btechMSFT261.470.9326.9819420.95333.00
cengrXOM93.871.0810.273913.68103.28
dengrCOP109.451.388.871371.0075.00
efinBAC33.471.4010.462692.6242.41
ffinJPM114.511.109.183363.52138.80
gfinWMT335.501.397.531143.00394.07
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 273 } ] }, { "cell_type": "markdown", "source": [ "### chekcking data contents" ], "metadata": { "id": "rkpxOiSvB4R1" } }, { "cell_type": "code", "source": [ "stock.info()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "PvBIWnex4wP5", "outputId": "589f2cf7-8a40-4bdf-9b8b-b23c1fad794d" }, "execution_count": 274, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "Index: 7 entries, a to g\n", "Data columns (total 8 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 sector 7 non-null object \n", " 1 ticker 7 non-null object \n", " 2 price 7 non-null float64\n", " 3 beta 7 non-null float64\n", " 4 pe 7 non-null float64\n", " 5 mktcap 7 non-null int64 \n", " 6 dy 7 non-null float64\n", " 7 target 7 non-null float64\n", "dtypes: float64(5), int64(1), object(2)\n", "memory usage: 504.0+ bytes\n" ] } ] }, { "cell_type": "code", "source": [ "stock.index = [0, 1, 2, 3, 4, 5, 6]\n", "stock" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "iBS9ehMS9p9p", "outputId": "70b0a7ba-2281-4d22-9c4e-32df756e809c" }, "execution_count": 275, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy target\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "2 engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "3 engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "5 fin JPM 114.51 1.10 9.18 336 3.52 138.80\n", "6 fin WMT 335.50 1.39 7.53 114 3.00 394.07" ], "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", " \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", "
sectortickerpricebetapemktcapdytarget
0techAAPL157.251.2326.1125390.58184.01
1techMSFT261.470.9326.9819420.95333.00
2engrXOM93.871.0810.273913.68103.28
3engrCOP109.451.388.871371.0075.00
4finBAC33.471.4010.462692.6242.41
5finJPM114.511.109.183363.52138.80
6finWMT335.501.397.531143.00394.07
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 275 } ] }, { "cell_type": "markdown", "source": [ "### dropping column(s)" ], "metadata": { "id": "wpSFI6zCCE_u" } }, { "cell_type": "code", "metadata": { "outputId": "b85ce900-afb8-4f42-843a-5f3fa0b2f26e", "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "IKTMBme4BgHY" }, "source": [ "stock.drop('target',axis=1)" ], "execution_count": 276, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95\n", "2 engr XOM 93.87 1.08 10.27 391 3.68\n", "3 engr COP 109.45 1.38 8.87 137 1.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62\n", "5 fin JPM 114.51 1.10 9.18 336 3.52\n", "6 fin WMT 335.50 1.39 7.53 114 3.00" ], "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", " \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", "
sectortickerpricebetapemktcapdy
0techAAPL157.251.2326.1125390.58
1techMSFT261.470.9326.9819420.95
2engrXOM93.871.0810.273913.68
3engrCOP109.451.388.871371.00
4finBAC33.471.4010.462692.62
5finJPM114.511.109.183363.52
6finWMT335.501.397.531143.00
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 276 } ] }, { "cell_type": "code", "source": [ "# but this wont affect the orginal data\n", "stock" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "BZftp4evCRps", "outputId": "62dfbae7-9e3c-4b54-b6c5-2300f7d448ec" }, "execution_count": 277, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy target\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "2 engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "3 engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "5 fin JPM 114.51 1.10 9.18 336 3.52 138.80\n", "6 fin WMT 335.50 1.39 7.53 114 3.00 394.07" ], "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", " \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", "
sectortickerpricebetapemktcapdytarget
0techAAPL157.251.2326.1125390.58184.01
1techMSFT261.470.9326.9819420.95333.00
2engrXOM93.871.0810.273913.68103.28
3engrCOP109.451.388.871371.0075.00
4finBAC33.471.4010.462692.6242.41
5finJPM114.511.109.183363.52138.80
6finWMT335.501.397.531143.00394.07
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 277 } ] }, { "cell_type": "code", "source": [ "# must put down inplace option\n", "stock.drop('target', axis=1, inplace=True)\n", "stock" ], "metadata": { "id": "nyXqYMo8RwLg", "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "outputId": "837ae5ee-89d2-499d-e26c-9c8e1e9fe4ad" }, "execution_count": 278, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95\n", "2 engr XOM 93.87 1.08 10.27 391 3.68\n", "3 engr COP 109.45 1.38 8.87 137 1.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62\n", "5 fin JPM 114.51 1.10 9.18 336 3.52\n", "6 fin WMT 335.50 1.39 7.53 114 3.00" ], "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", " \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", "
sectortickerpricebetapemktcapdy
0techAAPL157.251.2326.1125390.58
1techMSFT261.470.9326.9819420.95
2engrXOM93.871.0810.273913.68
3engrCOP109.451.388.871371.00
4finBAC33.471.4010.462692.62
5finJPM114.511.109.183363.52
6finWMT335.501.397.531143.00
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 278 } ] }, { "cell_type": "code", "source": [ "stock['target'] = [184.01, 333.00, 103.28, 75.00, 42.41, 138.80, 394.07]\n", "stock" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "xxgCpl0-RwH9", "outputId": "e79b83d8-7611-45e6-e2bb-01f710947d7e" }, "execution_count": 279, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy target\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "2 engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "3 engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "5 fin JPM 114.51 1.10 9.18 336 3.52 138.80\n", "6 fin WMT 335.50 1.39 7.53 114 3.00 394.07" ], "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", " \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", "
sectortickerpricebetapemktcapdytarget
0techAAPL157.251.2326.1125390.58184.01
1techMSFT261.470.9326.9819420.95333.00
2engrXOM93.871.0810.273913.68103.28
3engrCOP109.451.388.871371.0075.00
4finBAC33.471.4010.462692.6242.41
5finJPM114.511.109.183363.52138.80
6finWMT335.501.397.531143.00394.07
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 279 } ] }, { "cell_type": "code", "source": [ "stock2 = stock.drop(6, axis=0)\n", "stock2" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 238 }, "id": "UgLiTDfvRwEN", "outputId": "610a6f2f-5963-4773-fac0-312bc365b5ad" }, "execution_count": 280, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy target\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "2 engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "3 engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "5 fin JPM 114.51 1.10 9.18 336 3.52 138.80" ], "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", " \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", "
sectortickerpricebetapemktcapdytarget
0techAAPL157.251.2326.1125390.58184.01
1techMSFT261.470.9326.9819420.95333.00
2engrXOM93.871.0810.273913.68103.28
3engrCOP109.451.388.871371.0075.00
4finBAC33.471.4010.462692.6242.41
5finJPM114.511.109.183363.52138.80
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 280 } ] }, { "cell_type": "code", "source": [ "newstock = pd.DataFrame([['ind', 184.71, 1.00, 14.61, 98, 2.63, 214.00]], \n", " index=['CAT'],\n", " columns=['sector', 'price', 'beta', 'pe', 'mktcap', 'dy', 'target'])\n", " \n", "newstock" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 81 }, "id": "wDUx_fPbF8LX", "outputId": "35d6b4da-6e60-42cf-df4c-9019961cccb5" }, "execution_count": 281, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector price beta pe mktcap dy target\n", "CAT ind 184.71 1.0 14.61 98 2.63 214.0" ], "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", "
sectorpricebetapemktcapdytarget
CATind184.711.014.61982.63214.0
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 281 } ] }, { "cell_type": "code", "source": [ "stock3 = stock.append(newstock)\n", "stock3" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 300 }, "id": "dsloyDS8F8IK", "outputId": "022e73a3-c325-4e2e-acfa-64a9265a0845" }, "execution_count": 282, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy target\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "2 engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "3 engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "5 fin JPM 114.51 1.10 9.18 336 3.52 138.80\n", "6 fin WMT 335.50 1.39 7.53 114 3.00 394.07\n", "CAT ind NaN 184.71 1.00 14.61 98 2.63 214.00" ], "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sectortickerpricebetapemktcapdytarget
0techAAPL157.251.2326.1125390.58184.01
1techMSFT261.470.9326.9819420.95333.00
2engrXOM93.871.0810.273913.68103.28
3engrCOP109.451.388.871371.0075.00
4finBAC33.471.4010.462692.6242.41
5finJPM114.511.109.183363.52138.80
6finWMT335.501.397.531143.00394.07
CATindNaN184.711.0014.61982.63214.00
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 282 } ] }, { "cell_type": "code", "source": [ "stock3 = pd.concat([stock, newstock])\n", "stock3" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 300 }, "id": "KrkBCYUuEaGo", "outputId": "14493cf9-d298-4735-9a81-891e6f9894db" }, "execution_count": 283, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy target\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "2 engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "3 engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "5 fin JPM 114.51 1.10 9.18 336 3.52 138.80\n", "6 fin WMT 335.50 1.39 7.53 114 3.00 394.07\n", "CAT ind NaN 184.71 1.00 14.61 98 2.63 214.00" ], "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sectortickerpricebetapemktcapdytarget
0techAAPL157.251.2326.1125390.58184.01
1techMSFT261.470.9326.9819420.95333.00
2engrXOM93.871.0810.273913.68103.28
3engrCOP109.451.388.871371.0075.00
4finBAC33.471.4010.462692.6242.41
5finJPM114.511.109.183363.52138.80
6finWMT335.501.397.531143.00394.07
CATindNaN184.711.0014.61982.63214.00
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 283 } ] }, { "cell_type": "markdown", "source": [ "### replacing value" ], "metadata": { "id": "AdDOQ-9DCOgu" } }, { "cell_type": "code", "source": [ "stock.loc[6, 'ticker'] = 'GS'\n", "stock" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "gTvd4WAP_XG_", "outputId": "3f13ee59-417e-4cb8-d634-d89a9b495b6b" }, "execution_count": 284, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy target\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "2 engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "3 engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "5 fin JPM 114.51 1.10 9.18 336 3.52 138.80\n", "6 fin GS 335.50 1.39 7.53 114 3.00 394.07" ], "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", " \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", "
sectortickerpricebetapemktcapdytarget
0techAAPL157.251.2326.1125390.58184.01
1techMSFT261.470.9326.9819420.95333.00
2engrXOM93.871.0810.273913.68103.28
3engrCOP109.451.388.871371.0075.00
4finBAC33.471.4010.462692.6242.41
5finJPM114.511.109.183363.52138.80
6finGS335.501.397.531143.00394.07
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 284 } ] }, { "cell_type": "code", "source": [ "stock['price'].replace(335.50, 435.50, inplace=True)" ], "metadata": { "id": "wAJJCTVCSglz" }, "execution_count": 285, "outputs": [] }, { "cell_type": "code", "source": [ "stock" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "hlMd_zl5Sgeb", "outputId": "1e9d10dc-238a-4268-efea-40f75bde6205" }, "execution_count": 286, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy target\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "2 engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "3 engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "5 fin JPM 114.51 1.10 9.18 336 3.52 138.80\n", "6 fin GS 435.50 1.39 7.53 114 3.00 394.07" ], "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", " \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", "
sectortickerpricebetapemktcapdytarget
0techAAPL157.251.2326.1125390.58184.01
1techMSFT261.470.9326.9819420.95333.00
2engrXOM93.871.0810.273913.68103.28
3engrCOP109.451.388.871371.0075.00
4finBAC33.471.4010.462692.6242.41
5finJPM114.511.109.183363.52138.80
6finGS435.501.397.531143.00394.07
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 286 } ] }, { "cell_type": "markdown", "source": [ "### selecting row or column or both" ], "metadata": { "id": "wNnApZD-SZcy" } }, { "cell_type": "code", "source": [ "print(stock['ticker']); print('\\n');\n", "print(stock[['ticker']]); print('\\n');\n", "print(stock.ticker); print('\\n');\n", "print(stock.loc[:, 'ticker']); print('\\n');\n", "print(stock.iloc[:, 1]); print('\\n');\n", "print(stock.iloc[:, -1]); print('\\n');" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "nsJpZEu79p6T", "outputId": "3c053e28-e246-4545-cb3f-adc9f619dd0c" }, "execution_count": 287, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "0 AAPL\n", "1 MSFT\n", "2 XOM\n", "3 COP\n", "4 BAC\n", "5 JPM\n", "6 GS\n", "Name: ticker, dtype: object\n", "\n", "\n", " ticker\n", "0 AAPL\n", "1 MSFT\n", "2 XOM\n", "3 COP\n", "4 BAC\n", "5 JPM\n", "6 GS\n", "\n", "\n", "0 AAPL\n", "1 MSFT\n", "2 XOM\n", "3 COP\n", "4 BAC\n", "5 JPM\n", "6 GS\n", "Name: ticker, dtype: object\n", "\n", "\n", "0 AAPL\n", "1 MSFT\n", "2 XOM\n", "3 COP\n", "4 BAC\n", "5 JPM\n", "6 GS\n", "Name: ticker, dtype: object\n", "\n", "\n", "0 AAPL\n", "1 MSFT\n", "2 XOM\n", "3 COP\n", "4 BAC\n", "5 JPM\n", "6 GS\n", "Name: ticker, dtype: object\n", "\n", "\n", "0 184.01\n", "1 333.00\n", "2 103.28\n", "3 75.00\n", "4 42.41\n", "5 138.80\n", "6 394.07\n", "Name: target, dtype: float64\n", "\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "stock.loc[:, 'ticker':'beta']" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "phMyk25w9FwK", "outputId": "dd1f4029-8a36-481d-8828-3624261c3ca0" }, "execution_count": 288, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " ticker price beta\n", "0 AAPL 157.25 1.23\n", "1 MSFT 261.47 0.93\n", "2 XOM 93.87 1.08\n", "3 COP 109.45 1.38\n", "4 BAC 33.47 1.40\n", "5 JPM 114.51 1.10\n", "6 GS 435.50 1.39" ], "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
tickerpricebeta
0AAPL157.251.23
1MSFT261.470.93
2XOM93.871.08
3COP109.451.38
4BAC33.471.40
5JPM114.511.10
6GS435.501.39
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 288 } ] }, { "cell_type": "code", "source": [ "stock.loc[:, :]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 269 }, "id": "BXUQK-DX9UX0", "outputId": "4b419a14-5ee7-4372-9ace-16580f404478" }, "execution_count": 289, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector ticker price beta pe mktcap dy target\n", "0 tech AAPL 157.25 1.23 26.11 2539 0.58 184.01\n", "1 tech MSFT 261.47 0.93 26.98 1942 0.95 333.00\n", "2 engr XOM 93.87 1.08 10.27 391 3.68 103.28\n", "3 engr COP 109.45 1.38 8.87 137 1.00 75.00\n", "4 fin BAC 33.47 1.40 10.46 269 2.62 42.41\n", "5 fin JPM 114.51 1.10 9.18 336 3.52 138.80\n", "6 fin GS 435.50 1.39 7.53 114 3.00 394.07" ], "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", " \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", "
sectortickerpricebetapemktcapdytarget
0techAAPL157.251.2326.1125390.58184.01
1techMSFT261.470.9326.9819420.95333.00
2engrXOM93.871.0810.273913.68103.28
3engrCOP109.451.388.871371.0075.00
4finBAC33.471.4010.462692.6242.41
5finJPM114.511.109.183363.52138.80
6finGS435.501.397.531143.00394.07
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 289 } ] }, { "cell_type": "code", "source": [ "stock.loc[0:2, 'beta']" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "k-zwM-neKpjv", "outputId": "060c8ed0-19e3-44de-d412-518e90ea5c15" }, "execution_count": 290, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0 1.23\n", "1 0.93\n", "2 1.08\n", "Name: beta, dtype: float64" ] }, "metadata": {}, "execution_count": 290 } ] }, { "cell_type": "code", "source": [ "stock.loc[3, \"ticker\"]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "FCxL2WC99UU_", "outputId": "d17ea4e7-2f6c-4d11-cd82-010cb846e7f3" }, "execution_count": 291, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'COP'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 291 } ] }, { "cell_type": "code", "source": [ "stock.loc[5, \"mktcap\"] > stock.loc[6, \"mktcap\"]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "EKb9Mr3U9URz", "outputId": "0886ca21-8cd1-4ad5-aa8c-433f0be747a6" }, "execution_count": 292, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 292 } ] }, { "cell_type": "code", "source": [ "stock.index" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vJ_5U42E_aF6", "outputId": "f9feb699-a0ab-421c-db57-22c1d23b3bf7" }, "execution_count": 293, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Int64Index([0, 1, 2, 3, 4, 5, 6], dtype='int64')" ] }, "metadata": {}, "execution_count": 293 } ] }, { "cell_type": "markdown", "source": [ "### data summary" ], "metadata": { "id": "BkrZsts0T1Ap" } }, { "cell_type": "code", "source": [ "stock.describe()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 300 }, "id": "icmRehpU9p3A", "outputId": "5d12f111-817a-44d3-8ff8-372fbd8d9f73" }, "execution_count": 294, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " price beta pe mktcap dy target\n", "count 7.000000 7.000000 7.000000 7.000000 7.000000 7.000000\n", "mean 172.217143 1.215714 14.200000 818.285714 2.192857 181.510000\n", "std 135.503274 0.184829 8.492224 991.686058 1.315025 133.400706\n", "min 33.470000 0.930000 7.530000 114.000000 0.580000 42.410000\n", "25% 101.660000 1.090000 9.025000 203.000000 0.975000 89.140000\n", "50% 114.510000 1.230000 10.270000 336.000000 2.620000 138.800000\n", "75% 209.360000 1.385000 18.285000 1166.500000 3.260000 258.505000\n", "max 435.500000 1.400000 26.980000 2539.000000 3.680000 394.070000" ], "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", " \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", "
pricebetapemktcapdytarget
count7.0000007.0000007.0000007.0000007.0000007.000000
mean172.2171431.21571414.200000818.2857142.192857181.510000
std135.5032740.1848298.492224991.6860581.315025133.400706
min33.4700000.9300007.530000114.0000000.58000042.410000
25%101.6600001.0900009.025000203.0000000.97500089.140000
50%114.5100001.23000010.270000336.0000002.620000138.800000
75%209.3600001.38500018.2850001166.5000003.260000258.505000
max435.5000001.40000026.9800002539.0000003.680000394.070000
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 294 } ] }, { "cell_type": "code", "source": [ "print(stock['price'].mean()); \n", "print(stock['beta'].median()); \n", "print(stock['dy'].std()); \n", "print(stock['mktcap'].max()); " ], "metadata": { "id": "4a6WIB149p0K", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "4472f943-468c-47e5-c5ca-f72f244446f4" }, "execution_count": 295, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "172.21714285714285\n", "1.23\n", "1.315024895654252\n", "2539\n" ] } ] }, { "cell_type": "code", "source": [ "stock.groupby('sector').mean().round(2)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 175 }, "id": "BgjHuJhbVbGQ", "outputId": "f92d81f2-00cd-4754-b3a6-8e583203ae78" }, "execution_count": 296, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " price beta pe mktcap dy target\n", "sector \n", "engr 101.66 1.23 9.57 264.00 2.34 89.14\n", "fin 194.49 1.30 9.06 239.67 3.05 191.76\n", "tech 209.36 1.08 26.54 2240.50 0.76 258.50" ], "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", " \n", " \n", " \n", " \n", " \n", " \n", "
pricebetapemktcapdytarget
sector
engr101.661.239.57264.002.3489.14
fin194.491.309.06239.673.05191.76
tech209.361.0826.542240.500.76258.50
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 296 } ] }, { "cell_type": "code", "source": [ "stock.set_index('ticker', inplace=True)\n" ], "metadata": { "id": "VRZPar-T-TAw" }, "execution_count": 297, "outputs": [] }, { "cell_type": "code", "source": [ "stock.loc['JPM', 'mktcap'] > stock.loc['GS', 'mktcap']" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cc43yQ-T-gf9", "outputId": "5dcfda14-0cec-4d8a-d8d3-f1db23b349c3" }, "execution_count": 298, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 298 } ] }, { "cell_type": "code", "source": [ "stock" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 300 }, "id": "ZXYu_YY_MwNZ", "outputId": "dc1dfbd3-ddd6-4382-9e81-9973ea16d017" }, "execution_count": 299, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " sector price beta pe mktcap dy target\n", "ticker \n", "AAPL tech 157.25 1.23 26.11 2539 0.58 184.01\n", "MSFT tech 261.47 0.93 26.98 1942 0.95 333.00\n", "XOM engr 93.87 1.08 10.27 391 3.68 103.28\n", "COP engr 109.45 1.38 8.87 137 1.00 75.00\n", "BAC fin 33.47 1.40 10.46 269 2.62 42.41\n", "JPM fin 114.51 1.10 9.18 336 3.52 138.80\n", "GS fin 435.50 1.39 7.53 114 3.00 394.07" ], "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", " \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", " \n", " \n", "
sectorpricebetapemktcapdytarget
ticker
AAPLtech157.251.2326.1125390.58184.01
MSFTtech261.470.9326.9819420.95333.00
XOMengr93.871.0810.273913.68103.28
COPengr109.451.388.871371.0075.00
BACfin33.471.4010.462692.6242.41
JPMfin114.511.109.183363.52138.80
GSfin435.501.397.531143.00394.07
\n", "
\n", " \n", " \n", " \n", "\n", " \n", "
\n", "
\n", " " ] }, "metadata": {}, "execution_count": 299 } ] } ] }