Created
April 28, 2020 11:39
-
-
Save keimina/fc606a432895b660946edaad55bdd956 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 a|A\n", | |
"1 b|B\n", | |
"dtype: object" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s1 = pd.Series([\"a\", \"b\"])\n", | |
"s2 = pd.Series([\"A\", \"B\"])\n", | |
"s1.str.cat(s2, sep=\"|\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 1\n", | |
"1 1\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s1.str.len()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 True\n", | |
"1 False\n", | |
"dtype: bool" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s1.str.match(r'a')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"a 1 0\n", | |
" 2 1\n", | |
" 3 2\n", | |
"b 1 3\n", | |
" 2 4\n", | |
" 3 5\n", | |
"c 1 6\n", | |
" 2 7\n", | |
" 3 8\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s4 = pd.Series(np.arange(9), index=[[\"a\"]*3+[\"b\"]*3+[\"c\"]*3, [1,2,3,1,2,3,1,2,3]])\n", | |
"s4" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"pandas.core.series.Series" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(s4)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"pandas.core.series.Series" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s4[\"a\"]\n", | |
"type(s4[\"a\"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"pandas.core.indexes.multi.MultiIndex" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(s4.index)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"pandas.core.indexes.numeric.Int64Index" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(s4[\"a\"].index)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"pandas.core.indexes.numeric.Int64Index" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s5 = pd.Series(np.arange(9), [1,2,3,1,2,3,1,2,3])\n", | |
"type(s5.index)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"a 1 0\n", | |
" 2 1\n", | |
" 3 2\n", | |
"b 1 3\n", | |
" 2 4\n", | |
" 3 5\n", | |
"c 1 6\n", | |
" 2 7\n", | |
" 3 8\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s4" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"X Y\n", | |
"a 1 0\n", | |
" 2 1\n", | |
" 3 2\n", | |
"b 1 3\n", | |
" 2 4\n", | |
" 3 5\n", | |
"c 1 6\n", | |
" 2 7\n", | |
" 3 8\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s4.index.names = [\"X\", \"Y\"]\n", | |
"s4" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Y X\n", | |
"1 a 0\n", | |
"2 a 1\n", | |
"3 a 2\n", | |
"1 b 3\n", | |
"2 b 4\n", | |
"3 b 5\n", | |
"1 c 6\n", | |
"2 c 7\n", | |
"3 c 8\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s4.swaplevel('Y', 'X')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Y X\n", | |
"1 a 0\n", | |
" a 1\n", | |
"3 a 2\n", | |
"1 b 3\n", | |
" b 4\n", | |
"3 b 5\n", | |
"1 c 6\n", | |
" c 7\n", | |
"3 c 8\n", | |
"dtype: int64" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s6 = pd.Series(np.arange(9), index=[[\"a\"]*3+[\"b\"]*3+[\"c\"]*3, [1,1,3,1,1,3,1,1,3]])\n", | |
"s6.index.names = [\"X\", \"Y\"]\n", | |
"s6.swaplevel('Y', 'X')" | |
] | |
} | |
], | |
"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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment