pandas 기본 그룹

51456 단어
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np
"
, "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### group" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### " ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/html": [ "

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

", "
Hand sex IQ EQ
0 Right 201 62
1 Right 79 130
2 Right 175 426
3 Right 223 453
4 Right 247 359
... ... ... ... ...
195 Right 242 236
196 Right 196 257
197 Right 78 40
198 Right 122 256
199 Right 153 478

"
, "

200 rows × 4 columns


"
, "
"
], "text/plain": [ " Hand sex IQ EQ
"
, "0 Right 201 62
"
, "1 Right 79 130
"
, "2 Right 175 426
"
, "3 Right 223 453
"
, "4 Right 247 359
"
, ".. ... .. ... ...
"
, "195 Right 242 236
"
, "196 Right 196 257
"
, "197 Right 78 40
"
, "198 Right 122 256
"
, "199 Right 153 478
"
, "
"
, "[200 rows x 4 columns]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(data = {'Hand':np.random.randint(0,20,size = 200),
"
, " 'sex':np.random.randint(0,2,size = 200),
"
, " 'IQ':np.random.randint(35,256,size = 200),
"
, " 'EQ':np.random.randint(20,512,size = 200)})
"
, "#
"
, "def convert(x):
"
, " if x == 0:
"
, " return ' '
"
, " else:
"
, " return ' '
"
, "df['sex'] = df['sex'].apply(convert)
"
, "#
"
, "def convert2(x):
"
, " if x < 19:
"
, " return 'Right'
"
, " else:
"
, " return 'Left'
"
, "df['Hand'] = df['Hand'].apply(convert2)
"
, "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### , , " ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/html": [ "

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

", "
IQ EQ
Hand
", "

", "
Left 211 485
Right 255 505

"
, "
"
], "text/plain": [ " IQ EQ
"
, "Hand
"
, "Left 211 485
"
, "Right 255 505" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# , , ,IQ EQ
"
, "#
"
, "g = df.groupby(by = ['Hand'])[['IQ','EQ']]
"
, "g.max()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/html": [ "

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

", "
IQ EQ
Hand
", "

", "
Left 130.9 285.8
Right 146.9 271.2

"
, "
"
], "text/plain": [ " IQ EQ
"
, "Hand
"
, "Left 130.9 285.8
"
, "Right 146.9 271.2" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g = df.groupby(by = ['Hand'])[['IQ','EQ']]
"
, "g.mean().round(1)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/html": [ "

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

", "

", "
IQ EQ
Hand sex
", "

", "
Left 190 485
211 474
Right 255 505
255 503

"
, "
"
], "text/plain": [ " IQ EQ
"
, "Hand sex
"
, "Left 190 485
"
, " 211 474
"
, "Right 255 505
"
, " 255 503" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#
"
, "g = df.groupby(by = ['Hand','sex'])
"
, "g.max()" ] } ], "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 }

좋은 웹페이지 즐겨찾기