{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Series\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from pandas import Series, DataFrame\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "obj = pd. Series([1, 3, 5, -7, 9])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 1\n", "1 3\n", "2 5\n", "3 -7\n", "4 9\n", "dtype: int64" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 3, 5, -7, 9])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj.values" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RangeIndex(start=0, stop=5, step=1)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj.index\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "obj2 = pd.Series([2,4,6,-8,10], index=['a', 'b', 'c', 'd', 'e'])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 2\n", "b 4\n", "c 6\n", "d -8\n", "e 10\n", "dtype: int64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj2" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj2['a']" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b 4\n", "c 6\n", "d -8\n", "dtype: int64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj2[['b', 'c', 'd']]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 10\n", "b 20\n", "c 30\n", "d -40\n", "e 50\n", "dtype: int64" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj2 * 5\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 2\n", "b 4\n", "c 6\n", "e 10\n", "dtype: int64" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj2[obj2 > 0] " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "d -8\n", "dtype: int64" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj2[obj2 < 0]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "e 10\n", "dtype: int64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj2[obj2 > 8]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 7.389056\n", "b 54.598150\n", "c 403.428793\n", "d 0.000335\n", "e 22026.465795\n", "dtype: float64" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.exp(obj2)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "population_dict = {'Nordrhein-Westfalen': 17933000, 'Bayern': 13077000, \n", " 'Baden-Württemberg': 11070000, 'Niedersachsen': 7982000}" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "obj3 = pd.Series(population_dict)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Nordrhein-Westfalen 17933000\n", "Bayern 13077000\n", "Baden-Württemberg 11070000\n", "Niedersachsen 7982000\n", "dtype: int64" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj3" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13077000" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj3['Bayern']" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Bayern 13077000\n", "Baden-Württemberg 11070000\n", "Niedersachsen 7982000\n", "dtype: int64" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj3['Bayern':'Niedersachsen']" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "states = ['Berlin','Bayern', 'Niedersachsen', 'Baden-Württemberg']" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "obj4 = pd.Series(population_dict, index=states)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Berlin NaN\n", "Bayern 13077000.0\n", "Niedersachsen 7982000.0\n", "Baden-Württemberg 11070000.0\n", "dtype: float64" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj4" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Berlin True\n", "Bayern False\n", "Niedersachsen False\n", "Baden-Württemberg False\n", "dtype: bool" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.isnull(obj4)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Berlin False\n", "Bayern True\n", "Niedersachsen True\n", "Baden-Württemberg True\n", "dtype: bool" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.notnull(obj4)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Berlin True\n", "Bayern False\n", "Niedersachsen False\n", "Baden-Württemberg False\n", "dtype: bool" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj4.isnull()" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Nordrhein-Westfalen 17933000\n", "Bayern 13077000\n", "Baden-Württemberg 11070000\n", "Niedersachsen 7982000\n", "dtype: int64" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj3\n" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Berlin NaN\n", "Bayern 13077000.0\n", "Niedersachsen 7982000.0\n", "Baden-Württemberg 11070000.0\n", "dtype: float64" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj4" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Baden-Württemberg 22140000.0\n", "Bayern 26154000.0\n", "Berlin NaN\n", "Niedersachsen 15964000.0\n", "Nordrhein-Westfalen NaN\n", "dtype: float64" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj3 + obj4" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "obj4.name = 'population'" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "obj4.index.name = 'state'" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "state\n", "Berlin NaN\n", "Bayern 13077000.0\n", "Niedersachsen 7982000.0\n", "Baden-Württemberg 11070000.0\n", "Name: population, dtype: float64" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "obj4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### DataFrame" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "areacode_dict = {'Nordrhein-Westfalen': 3, 'Bayern': 8, 'Baden-Württemberg': 7, 'Niedersachsen': 3 }" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "areacode = pd.Series(areacode_dict)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Nordrhein-Westfalen 3\n", "Bayern 8\n", "Baden-Württemberg 7\n", "Niedersachsen 3\n", "dtype: int64" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "areacode" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "population = obj3" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "states = pd.DataFrame({'population': population, 'areacode': areacode})" ] }, { "cell_type": "code", "execution_count": 39, "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", "
populationareacode
Nordrhein-Westfalen179330003
Bayern130770008
Baden-Württemberg110700007
Niedersachsen79820003
\n", "
" ], "text/plain": [ " population areacode\n", "Nordrhein-Westfalen 17933000 3\n", "Bayern 13077000 8\n", "Baden-Württemberg 11070000 7\n", "Niedersachsen 7982000 3" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "states" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['Nordrhein-Westfalen', 'Bayern', 'Baden-Württemberg', 'Niedersachsen'], dtype='object')" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "states.index" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['population', 'areacode'], dtype='object')" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "states.columns" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Nordrhein-Westfalen 3\n", "Bayern 8\n", "Baden-Württemberg 7\n", "Niedersachsen 3\n", "Name: areacode, dtype: int64" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "states['areacode']" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "data = [{'x': i, 'y': 4 * i}\n", " for i in range(4)]\n" ] }, { "cell_type": "code", "execution_count": 44, "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", "
xy
000
114
228
3312
\n", "
" ], "text/plain": [ " x y\n", "0 0 0\n", "1 1 4\n", "2 2 8\n", "3 3 12" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame(data)\n" ] }, { "cell_type": "code", "execution_count": 45, "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", "
population
Nordrhein-Westfalen17933000
Bayern13077000
Baden-Württemberg11070000
Niedersachsen7982000
\n", "
" ], "text/plain": [ " population\n", "Nordrhein-Westfalen 17933000\n", "Bayern 13077000\n", "Baden-Württemberg 11070000\n", "Niedersachsen 7982000" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame(population, columns=['population'])" ] }, { "cell_type": "code", "execution_count": 46, "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", "
populationareacode
Nordrhein-Westfalen179330003
Bayern130770008
Baden-Württemberg110700007
Niedersachsen79820003
\n", "
" ], "text/plain": [ " population areacode\n", "Nordrhein-Westfalen 17933000 3\n", "Bayern 13077000 8\n", "Baden-Württemberg 11070000 7\n", "Niedersachsen 7982000 3" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame({'population': population, 'areacode': areacode})" ] }, { "cell_type": "code", "execution_count": 47, "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", "
ab
x0.8118150.630333
y0.4805160.370507
z0.7421640.799571
\n", "
" ], "text/plain": [ " a b\n", "x 0.811815 0.630333\n", "y 0.480516 0.370507\n", "z 0.742164 0.799571" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame(np.random.rand(3, 2), \n", " columns=['a', 'b'],\n", " index=['x', 'y', 'z'])" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "A = np.zeros(3, dtype=[('A', 'i8'), ('B', 'f8')])" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([(0, 0.), (0, 0.), (0, 0.)], dtype=[('A', '\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AB
000.0
100.0
200.0
\n", "" ], "text/plain": [ " A B\n", "0 0 0.0\n", "1 0 0.0\n", "2 0 0.0" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame(A)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }