pandas 재구성

53732 단어
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np
"
, "
"
, "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### unstack" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "(10,)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "A 29
"
, " 60
"
, "B 137
"
, " 14
"
, "C 68
"
, " 5
"
, "D 83
"
, " 34
"
, "E 29
"
, " 127
"
, "Name: Python, dtype: int32" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'-----------------------'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(5, 2)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "

", "
"
, "
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","

", "
A 29 60
B 137 14
C 68 5
D 83 34
E 29 127

"
, "
"
], "text/plain": [ "
"
, "A 29 60
"
, "B 137 14
"
, "C 68 5
"
, "D 83 34
"
, "E 29 127" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# pd.MultiIndex.from_product ,
"
, "s = pd.Series(np.random.randint(0,151,size = 10),
"
, " index = pd.MultiIndex.from_product([list('ABCDE'),[' ',' ']]),
"
, " name = 'Python')
"
, "# , ,== numpy.reshape
"
, "# unstack stack
"
, "# , ,
"
, "# unstack
"
, "df = s.unstack()
"
, "display(s.shape,s,'-----------------------',df.shape,df)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### stack" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/html": [ "

", "
"
, "
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","

", "
Python En
A 49 137 71
B 68 50 146
C 110 137 45
D 33 49 102
E 13 37 26

"
, "
"
], "text/plain": [ " Python En
"
, "A 49 137 71
"
, "B 68 50 146
"
, "C 110 137 45
"
, "D 33 49 102
"
, "E 13 37 26" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(15,)" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "A Python 49
"
, " 137
"
, " En 71
"
, "B Python 68
"
, " 50
"
, " En 146
"
, "C Python 110
"
, " 137
"
, " En 45
"
, "D Python 33
"
, " 49
"
, " En 102
"
, "E Python 13
"
, " 37
"
, " En 26
"
, "dtype: int32" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df2 = pd.DataFrame(np.random.randint(0,151,size = (5,3)),
"
, " index = list('ABCDE'),
"
, " columns=['Python',' ','En'])
"
, "
"
, "# , , reshape
"
, "s2 = df2.stack()
"
, "display(df2,s2.shape,s2)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "

", "
"
, "
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","

", "
A B C D E
Python 49 68 110 33 13
137 50 137 49 37
En 71 146 45 102 26

"
, "
"
], "text/plain": [ " A B C D E
"
, "Python 49 68 110 33 13
"
, " 137 50 137 49 37
"
, "En 71 146 45 102 26" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#
"
, "df2.T" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "

", "
"
, "
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","
","

", "
A B C D E
Python 49 68 110 33 13
137 50 137 49 37
En 71 146 45 102 26

"
, "
"
], "text/plain": [ " A B C D E
"
, "Python 49 68 110 33 13
"
, " 137 50 137 49 37
"
, "En 71 146 45 102 26" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2.stack().unstack(level = 0)" ] } ], "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.8.1" } }, "nbformat": 4, "nbformat_minor": 4 }

좋은 웹페이지 즐겨찾기