|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + " country capital area population\n", |
| 13 | + "0 Brazil Brasilia 8.516 200.40\n", |
| 14 | + "1 Russia Moscow 17.100 143.50\n", |
| 15 | + "2 India New Dehli 3.286 1252.00\n", |
| 16 | + "3 China Beijing 9.597 1357.00\n", |
| 17 | + "4 South Africa Pretoria 1.221 52.98\n" |
| 18 | + ] |
| 19 | + } |
| 20 | + ], |
| 21 | + "source": [ |
| 22 | + "dict = {\"country\": [\"Brazil\", \"Russia\", \"India\", \"China\", \"South Africa\"],\n", |
| 23 | + " \"capital\": [\"Brasilia\", \"Moscow\", \"New Dehli\", \"Beijing\", \"Pretoria\"],\n", |
| 24 | + " \"area\": [8.516, 17.10, 3.286, 9.597, 1.221],\n", |
| 25 | + " \"population\": [200.4, 143.5, 1252, 1357, 52.98] }\n", |
| 26 | + "\n", |
| 27 | + "import pandas as pd\n", |
| 28 | + "brics = pd.DataFrame(dict)\n", |
| 29 | + "print(brics)" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": 7, |
| 35 | + "metadata": {}, |
| 36 | + "outputs": [ |
| 37 | + { |
| 38 | + "name": "stdout", |
| 39 | + "output_type": "stream", |
| 40 | + "text": [ |
| 41 | + " country capital area population\n", |
| 42 | + "v Brazil Brasilia 8.516 200.40\n", |
| 43 | + "i Russia Moscow 17.100 143.50\n", |
| 44 | + "s India New Dehli 3.286 1252.00\n", |
| 45 | + "h China Beijing 9.597 1357.00\n", |
| 46 | + "a South Africa Pretoria 1.221 52.98\n", |
| 47 | + "\n", |
| 48 | + " Unnamed: 0 cars_per_cap country drives_right\n", |
| 49 | + "0 US 809 United States True\n", |
| 50 | + "1 AUS 731 Australia False\n", |
| 51 | + "2 JAP 588 Japan False\n", |
| 52 | + "3 IN 18 India False\n", |
| 53 | + "4 RU 200 Russia True\n", |
| 54 | + "5 MOR 70 Morocco True\n", |
| 55 | + "6 EG 45 Egypt True\n" |
| 56 | + ] |
| 57 | + } |
| 58 | + ], |
| 59 | + "source": [ |
| 60 | + "import pandas as pd\n", |
| 61 | + "brics.index = [\"v\",\"i\",\"s\",\"h\",\"a\"]\n", |
| 62 | + "print(brics)\n", |
| 63 | + "print()\n", |
| 64 | + "\n", |
| 65 | + "\n", |
| 66 | + "#bost = pd.read_csv('C://Users//Tushar Raj Shrma//Desktop//cars.csv')\n", |
| 67 | + "# both are working\n", |
| 68 | + "by = pd.read_csv('C:\\\\Users\\\\Tushar Raj Shrma\\\\Desktop\\\\cars.csv')\n", |
| 69 | + "print(by)" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 28, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [ |
| 77 | + { |
| 78 | + "name": "stdout", |
| 79 | + "output_type": "stream", |
| 80 | + "text": [ |
| 81 | + " student add father\n", |
| 82 | + "0 ram v 77\n", |
| 83 | + "1 shyam j 27\n", |
| 84 | + "2 vansh o 56\n", |
| 85 | + "3 joker e 99 \n", |
| 86 | + "\n", |
| 87 | + "\n", |
| 88 | + " student add father\n", |
| 89 | + "A ram v 77\n", |
| 90 | + "L shyam j 27\n", |
| 91 | + "E vansh o 56\n", |
| 92 | + "X joker e 99\n", |
| 93 | + "\n", |
| 94 | + "A 77\n", |
| 95 | + "L 27\n", |
| 96 | + "E 56\n", |
| 97 | + "X 99\n", |
| 98 | + "Name: father, dtype: int64\n", |
| 99 | + "\n", |
| 100 | + " father student\n", |
| 101 | + "A 77 ram\n", |
| 102 | + "L 27 shyam\n", |
| 103 | + "E 56 vansh\n", |
| 104 | + "X 99 joker\n", |
| 105 | + "\n", |
| 106 | + "\n", |
| 107 | + " student add father\n", |
| 108 | + "L shyam j 27\n", |
| 109 | + "E vansh o 56\n", |
| 110 | + "X joker e 99\n", |
| 111 | + "\n", |
| 112 | + "student vansh\n", |
| 113 | + "add o\n", |
| 114 | + "father 56\n", |
| 115 | + "Name: E, dtype: object\n", |
| 116 | + "\n" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "ename": "KeyError", |
| 121 | + "evalue": "\"None of [Index(['ram', 'joker'], dtype='object')] are in the [index]\"", |
| 122 | + "output_type": "error", |
| 123 | + "traceback": [ |
| 124 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 125 | + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", |
| 126 | + "\u001b[1;32m<ipython-input-28-22b39840783a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 21\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 22\u001b[0m \u001b[1;31m#print(cars.loc[['AUS', 'EG']])\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 23\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhasi\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'ram'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'joker'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", |
| 127 | + "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 1498\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1499\u001b[0m \u001b[0mmaybe_callable\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_if_callable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1500\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getitem_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmaybe_callable\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1501\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1502\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_is_scalar_access\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 128 | + "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m 1900\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Cannot index with multidimensional key'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1901\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1902\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getitem_iterable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1903\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1904\u001b[0m \u001b[1;31m# nested tuple slicing\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 129 | + "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_getitem_iterable\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m 1203\u001b[0m \u001b[1;31m# A collection of keys\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1204\u001b[0m keyarr, indexer = self._get_listlike_indexer(key, axis,\n\u001b[1;32m-> 1205\u001b[1;33m raise_missing=False)\n\u001b[0m\u001b[0;32m 1206\u001b[0m return self.obj._reindex_with_indexers({axis: [keyarr, indexer]},\n\u001b[0;32m 1207\u001b[0m copy=True, allow_dups=True)\n", |
| 130 | + "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_get_listlike_indexer\u001b[1;34m(self, key, axis, raise_missing)\u001b[0m\n\u001b[0;32m 1159\u001b[0m self._validate_read_indexer(keyarr, indexer,\n\u001b[0;32m 1160\u001b[0m \u001b[0mo\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_axis_number\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1161\u001b[1;33m raise_missing=raise_missing)\n\u001b[0m\u001b[0;32m 1162\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mkeyarr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindexer\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1163\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", |
| 131 | + "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_validate_read_indexer\u001b[1;34m(self, key, indexer, axis, raise_missing)\u001b[0m\n\u001b[0;32m 1244\u001b[0m raise KeyError(\n\u001b[0;32m 1245\u001b[0m u\"None of [{key}] are in the [{axis}]\".format(\n\u001b[1;32m-> 1246\u001b[1;33m key=key, axis=self.obj._get_axis_name(axis)))\n\u001b[0m\u001b[0;32m 1247\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1248\u001b[0m \u001b[1;31m# We (temporarily) allow for some missing keys with .loc, except in\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 132 | + "\u001b[1;31mKeyError\u001b[0m: \"None of [Index(['ram', 'joker'], dtype='object')] are in the [index]\"" |
| 133 | + ] |
| 134 | + } |
| 135 | + ], |
| 136 | + "source": [ |
| 137 | + "rule = {\"student\":[\"ram\",\"shyam\",\"vansh\",\"joker\"],\"add\":[\"v\",\"j\",\"o\",\"e\"],\"father\":[77,27,56,99]}\n", |
| 138 | + "import pandas as pd\n", |
| 139 | + "hasi = pd.DataFrame(rule)\n", |
| 140 | + "#in DataFrame (D and F must be in capital letter)\n", |
| 141 | + "print(hasi,\"\\n\")\n", |
| 142 | + "print()\n", |
| 143 | + "hasi.index = [\"A\",\"L\",\"E\",\"X\"]\n", |
| 144 | + "print(hasi)\n", |
| 145 | + "#hasi = pd.DataFrame(rule,index_col = 1)\n", |
| 146 | + "#print(hasi)\n", |
| 147 | + "print()\n", |
| 148 | + "\n", |
| 149 | + "print(hasi[\"father\"])\n", |
| 150 | + "print()\n", |
| 151 | + "print(hasi[[\"father\", \"student\"]])\n", |
| 152 | + "print()\n", |
| 153 | + "print()\n", |
| 154 | + "print(hasi[1:5])\n", |
| 155 | + "print()\n", |
| 156 | + "print(hasi.iloc[2]) # doubt\n", |
| 157 | + "print()\n", |
| 158 | + "#print(cars.loc[['AUS', 'EG']])\n", |
| 159 | + "print(hasi.loc[['ram','joker']]) # doubt" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "code", |
| 164 | + "execution_count": null, |
| 165 | + "metadata": {}, |
| 166 | + "outputs": [], |
| 167 | + "source": [] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": null, |
| 172 | + "metadata": {}, |
| 173 | + "outputs": [], |
| 174 | + "source": [] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": null, |
| 179 | + "metadata": {}, |
| 180 | + "outputs": [], |
| 181 | + "source": [] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "code", |
| 185 | + "execution_count": null, |
| 186 | + "metadata": {}, |
| 187 | + "outputs": [], |
| 188 | + "source": [] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": null, |
| 193 | + "metadata": {}, |
| 194 | + "outputs": [], |
| 195 | + "source": [] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": null, |
| 200 | + "metadata": {}, |
| 201 | + "outputs": [], |
| 202 | + "source": [] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "code", |
| 206 | + "execution_count": null, |
| 207 | + "metadata": {}, |
| 208 | + "outputs": [], |
| 209 | + "source": [] |
| 210 | + }, |
| 211 | + { |
| 212 | + "cell_type": "code", |
| 213 | + "execution_count": null, |
| 214 | + "metadata": {}, |
| 215 | + "outputs": [], |
| 216 | + "source": [] |
| 217 | + } |
| 218 | + ], |
| 219 | + "metadata": { |
| 220 | + "kernelspec": { |
| 221 | + "display_name": "Python 3", |
| 222 | + "language": "python", |
| 223 | + "name": "python3" |
| 224 | + }, |
| 225 | + "language_info": { |
| 226 | + "codemirror_mode": { |
| 227 | + "name": "ipython", |
| 228 | + "version": 3 |
| 229 | + }, |
| 230 | + "file_extension": ".py", |
| 231 | + "mimetype": "text/x-python", |
| 232 | + "name": "python", |
| 233 | + "nbconvert_exporter": "python", |
| 234 | + "pygments_lexer": "ipython3", |
| 235 | + "version": "3.7.3" |
| 236 | + } |
| 237 | + }, |
| 238 | + "nbformat": 4, |
| 239 | + "nbformat_minor": 2 |
| 240 | +} |
0 commit comments