{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 13wk-2: Pandas – `apply`, `map`, `applymap`\n", "\n", "최규빈 \n", "2023-11-29\n", "\n", "\n", "\n", "# 1. 강의영상\n", "\n", "\n", "\n", "# 2. Imports" ], "id": "0b03e077-5048-4cd4-9a38-fd12f7a00400" }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ], "id": "01fe1358-c052-4f83-8e8b-57e377b4aa16" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. `apply`\n", "\n", "## A. Motive\n", "\n", "`-` 아래와 같은 상황이 있었다. (05wk-2의 숙제)" ], "id": "d3407aef-a5c6-4a64-be33-021ff1ecd985" }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'A':[1,2,3,4]})\n", "df" ], "id": "2e676a1e-79b5-43d2-bbc8-6d12a5aad752" }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [], "source": [ "df[['A']].apply(np.mean)" ], "id": "5d93f62f-78b7-4343-9fa9-a48a44121176" }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [] }, "outputs": [], "source": [ "df['A'].apply(np.mean)" ], "id": "6cd54e47-4aaa-4276-b216-75effe1dfd0c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## B. `s.apply()`\n", "\n", "`-` 가능한 형태는 아래와 같다.\n", "\n", "1. 변환함수(스칼라입력,스칼라출력): 로그, 제곱\n", "2. 변환함수(벡터입력,벡터출력): 표준화, 정렬\n", "3. 집계함수(벡터입력,스칼라출력): 평균, 최대값\n", "\n", "쓸모있는건 1 뿐이다.\n", "\n", "`# 예제1` – `s.apply` + 스칼라입력, 스칼라출력" ], "id": "337d570e-2bcd-4eba-9f25-13f8da5e9457" }, { "cell_type": "code", "execution_count": 197, "metadata": { "tags": [] }, "outputs": [], "source": [ "s = pd.Series([1,2,3])\n", "s" ], "id": "cc4bb6de" }, { "cell_type": "code", "execution_count": 198, "metadata": { "tags": [] }, "outputs": [], "source": [ "s.apply(lambda x: -x)" ], "id": "95721c5a-0952-47f2-96d4-605863d91f32" }, { "cell_type": "markdown", "metadata": {}, "source": [ "이건 사실 아래의 동작으로 이해하면 된다.\n", "\n", " 1 -> -1 \n", " 2 -> -2\n", " 3 -> -3 \n", "\n", "코드로는 아래와 같은 느낌" ], "id": "32d2681c-9b36-4be9-b33b-89c5cee41e85" }, { "cell_type": "code", "execution_count": 199, "metadata": { "tags": [] }, "outputs": [], "source": [ "[(lambda x: -x)(i) for i in s]" ], "id": "97cfa4a4-98f7-4887-b1d7-3a8ebd3888ab" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제2` – `s.apply` + 벡터입력/스칼라출력(집계함수) // 가능은한데\n", "사실상 스칼라입력,스칼라출력으로 해석해야함" ], "id": "65837d77-1629-496a-acae-3e37b3a07800" }, { "cell_type": "code", "execution_count": 207, "metadata": { "tags": [] }, "outputs": [], "source": [ "s = pd.Series([1,2,3])\n", "s" ], "id": "e61b8986-9825-42b2-9953-1081c18e504b" }, { "cell_type": "code", "execution_count": 208, "metadata": { "tags": [] }, "outputs": [], "source": [ "s.apply(np.sum) # ??" ], "id": "c7dcf3fc-e945-44f1-a9e0-9f05c2e178e4" }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 에러는 안나지만 원하는 동작은 아님\n", "\n", "이것은 사실 아래의 동작으로 이해할 수 있다.\n", "\n", " 1 -> sum(1) = 1 \n", " 2 -> sum(2) = 2\n", " 3 -> sum(3) = 3\n", "\n", "코드로는 아래의 느낌" ], "id": "b3585e8e-8165-43c7-bb8f-7a279a7a796e" }, { "cell_type": "code", "execution_count": 43, "metadata": { "tags": [] }, "outputs": [], "source": [ "[np.sum(i) for i in s]" ], "id": "053f28f5-2e92-4b41-b0a8-518258e3ada5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제3` – `s.apply` + 벡터입력/벡터출력 // 가능은 한데 사실상\n", "스칼라입력,스칼라출력 함수로 해석해야함" ], "id": "fa76ee75-0f49-467b-99bd-651a908dfec3" }, { "cell_type": "code", "execution_count": 202, "metadata": { "tags": [] }, "outputs": [], "source": [ "s = pd.Series([1,2,3])\n", "s" ], "id": "315e2769-57a4-4358-b3af-a86586f90faa" }, { "cell_type": "code", "execution_count": 206, "metadata": { "tags": [] }, "outputs": [], "source": [ "s.apply(lambda x: x-np.mean(x))" ], "id": "f1aa6661-8915-4455-be76-2be26553dcad" }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 에러는 안나지만 원하는 동작은 아님\n", "\n", "이것은 사실 아래의 동작으로 이해할 수 있다.\n", "\n", " 1 -> 1-mean(1) = 0 \n", " 2 -> 2-mean(2) = 0\n", " 3 -> 3-mean(3) = 0\n", "\n", "코드로는 아래의 느낌" ], "id": "33f5bc08-358b-4c9b-a44e-12ddc1b60e0e" }, { "cell_type": "code", "execution_count": 205, "metadata": { "tags": [] }, "outputs": [], "source": [ "[i-np.mean(i) for i in s]" ], "id": "245bcdf1-1a0c-4454-8fbe-0fec2d8ce175" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "## C. `df.apply()`\n", "\n", "`-` 가능한 형태는 아래와 같다.\n", "\n", "1. 변환함수(벡터입력,벡터출력): 표준화, 정렬\n", "2. 집계함수(벡터입력,스칼라출력): 평균, 최대값\n", "\n", "쓸모있는건 1,2 모두이다.\n", "\n", "`# 예제1` – `df.apply` + 스칼라입력, 스칼라출력 (불가능)" ], "id": "7d7fadd5-a1e6-403b-8cf1-e8467fc39e29" }, { "cell_type": "code", "execution_count": 209, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'X':[0.1,0.2,0.3],'Y':[-0.1,-0.2,-0.3]})\n", "df" ], "id": "114f432e-6cab-42bc-8782-2f7e14c97ff1" }, { "cell_type": "code", "execution_count": 210, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.apply(lambda x: 'pos' if x>0 else 'neg')" ], "id": "f64bcffa-86a7-46a1-88f7-df63aea361ca" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`# 예제2` – `df.apply` + 스칼라입력,스칼라출력? 벡터입력,벡터출력!!" ], "id": "4e1a2be0-bb47-45aa-bd33-f86bbc910ce1" }, { "cell_type": "code", "execution_count": 211, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'X':[0.1,0.2,0.3],'Y':[-0.1,-0.2,-0.3]})\n", "df" ], "id": "952e14cb-5452-4472-a9de-e532bd524b67" }, { "cell_type": "code", "execution_count": 218, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.apply(lambda x: x**2) # 이건 스칼라입력, 스칼라출력 아니고 벡터입력 벡터출력으로 컴퓨터가 해석함" ], "id": "36989783-c858-4bbb-9845-ac4c753c8cb0" }, { "cell_type": "markdown", "metadata": {}, "source": [ "이것은 사실 아래의 동작으로 이해할 수 있다.\n", "\n", " df['X'] -> (df['X'])**2\n", " df['Y'] -> (df['Y'])**2\n", "\n", "코드로는 아래의 느낌이다." ], "id": "7707d37f-6fa1-4902-8609-b491585c5d32" }, { "cell_type": "code", "execution_count": 221, "metadata": { "tags": [] }, "outputs": [], "source": [ "[(lambda x: x**2)(df[i]) for i in df]" ], "id": "97b6373b-dfb6-4edc-abb5-4f1cc548f50b" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제3` – `df.apply` + 벡터입력,스칼라출력(집계함수)" ], "id": "b3392a89-9a57-4a72-b8ed-86040ffa2f08" }, { "cell_type": "code", "execution_count": 222, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'X':[0.1,0.2,0.3],'Y':[-0.1,-0.2,-0.3]})\n", "df" ], "id": "6b872348-b62a-4d1b-8738-0b3dd78c22e0" }, { "cell_type": "code", "execution_count": 223, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.apply(np.sum)" ], "id": "857f11f4-6522-4797-a88d-ff65fbc05cbf" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제4` – `df.apply` + 벡터입력,스칼라출력(집계함수)" ], "id": "50e105e8-08e5-4abc-af1c-07807cd705c0" }, { "cell_type": "code", "execution_count": 237, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'X':[0.1,0.2,0.3],'Y':[-0.1,-0.2,-0.3]})\n", "df" ], "id": "93d3d168-e8d0-4ee0-a5f8-e2a65e0e9aab" }, { "cell_type": "code", "execution_count": 238, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.apply(np.sum,axis=1)" ], "id": "52ee1b6a-3018-4550-8cb3-5fe433f9897c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "> `s.apply`에서는 `axis`가 유효한 인자가 아니지만 `df.apply`에서는\n", "> `axis`가 유효한 입력이고 디폴트값은 0이다.\n", "\n", "`#`\n", "\n", "`# 예제5` – `df.apply` + 벡터입력,벡터출력" ], "id": "2d64c56e-658e-4642-ad9e-33086d1c2a1e" }, { "cell_type": "code", "execution_count": 257, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'X':[1,2,3],'Y':[4,5,6]})\n", "df" ], "id": "21e68450-6c12-4405-8be2-9d04b6fc9d08" }, { "cell_type": "code", "execution_count": 259, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.apply(lambda x: x-np.mean(x))" ], "id": "b8495b52-3e7b-4e29-bf8f-f282cfa62884" }, { "cell_type": "code", "execution_count": 260, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.apply(lambda x: x-np.mean(x),axis=1)" ], "id": "60d2f011-632a-4320-956e-5795bcd4b17f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제6` – `df.apply` + 벡터입력, 벡터출력" ], "id": "aa9b61a6-b36d-49bd-8a63-0c7cd9e1f793" }, { "cell_type": "code", "execution_count": 264, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'X':[ 3.285, 0.328, -1.261],'Y':[ 1.068, 0.145, -0.222]})\n", "df" ], "id": "a22809a0-90d5-458a-b556-fe755858eda4" }, { "cell_type": "code", "execution_count": 273, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.apply(np.sort)" ], "id": "0fda0cb4-6ee2-4bcc-89bb-6f86b8a4f2be" }, { "cell_type": "code", "execution_count": 278, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.apply(np.sort, axis=1)" ], "id": "bd39f8e7-4edb-4260-b758-69f5d30ade2e" }, { "cell_type": "code", "execution_count": 279, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.apply(lambda x: x*0+np.sort(x), axis=1) # 그다지 안중요한 트릭.." ], "id": "eedb8f68-fc7c-49b2-97b0-ff12b8e2c1e6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "# 4. `map`\n", "\n", "`-` 그냥 모든 원소에 동일적용\n", "\n", "## A. `s.map()`\n", "\n", "`-` 가능한 형태는 아래와 같다.\n", "\n", "1. 변환함수(스칼라입력,스칼라출력): 로그, 제곱\n", "2. 변환함수(벡터입력,벡터출력): 표준화, 정렬\n", "3. 집계함수(벡터입력,스칼라출력): 평균, 최대값\n", "4. 딕셔너리\n", "\n", "쓸모있는건 1,4 이다. 특히 4는 특정상황에서 매우 쓸모있음\n", "\n", "`# 예제1` – `s.map` + 스칼라입력,스칼라출력" ], "id": "e2dae496-014c-4e97-80db-b731bc4e27eb" }, { "cell_type": "code", "execution_count": 281, "metadata": { "tags": [] }, "outputs": [], "source": [ "s = pd.Series(['A','B','B','B','A'])\n", "s" ], "id": "fdef7271-fc80-441e-9909-609b8dcb297d" }, { "cell_type": "code", "execution_count": 282, "metadata": { "tags": [] }, "outputs": [], "source": [ "s.map(lambda x: x.lower())" ], "id": "a66d975d-c47d-4579-bb57-d1b7cfaf2ca8" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제2` – `s.map` + 스칼라입력,스칼라출력" ], "id": "28ff058a-2935-4339-9a04-13797ca1bba4" }, { "cell_type": "code", "execution_count": 283, "metadata": { "tags": [] }, "outputs": [], "source": [ "s = pd.Series([1,3,4,2])\n", "s" ], "id": "0215c9e0-12f3-45c8-abca-7d44fa91c33b" }, { "cell_type": "code", "execution_count": 284, "metadata": { "tags": [] }, "outputs": [], "source": [ "s.map(lambda x: x**2)" ], "id": "ee2b7b13-777f-4f91-a48f-f48f3556743a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제3` – `s.map` + 벡터입력,스칼라출력 // 가능은한데 사실\n", "스칼라입력,스칼라출력으로 해석해야함" ], "id": "e32746df-2b96-476a-b5ca-aee8d4ea8707" }, { "cell_type": "code", "execution_count": 285, "metadata": { "tags": [] }, "outputs": [], "source": [ "s = pd.Series([1,3,4,2])\n", "s" ], "id": "ff1bbe2e-7a76-44dd-8e04-7b88588926c1" }, { "cell_type": "code", "execution_count": 161, "metadata": { "tags": [] }, "outputs": [], "source": [ "s.map(np.sum)" ], "id": "5dbfe9a3-052c-44dc-903c-6e2014ae7226" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제4` – `s.map` + 벡터입력,벡터출력 // 가능은한데 사실\n", "스칼라입력,스칼라출력으로 해석해야함" ], "id": "af23f119-8e71-446e-bb80-623ab9b78861" }, { "cell_type": "code", "execution_count": 287, "metadata": { "tags": [] }, "outputs": [], "source": [ "s = pd.Series([1,3,4,2])\n", "s" ], "id": "3db924ed-1b21-44c9-bcd7-f70c26d12b2f" }, { "cell_type": "code", "execution_count": 289, "metadata": { "tags": [] }, "outputs": [], "source": [ "s.map(lambda x: x-np.mean(x))" ], "id": "c4b047a8-6bbc-4d52-bdee-d19133e9178d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제5` – `s.map` + 딕셔너리" ], "id": "32da1560-7026-46d9-92bf-87a63897dd89" }, { "cell_type": "code", "execution_count": 290, "metadata": { "tags": [] }, "outputs": [], "source": [ "s = pd.Series(['A','B','B','B','A'])\n", "s" ], "id": "6b7b3386-25ac-433b-837e-16c84878ae48" }, { "cell_type": "code", "execution_count": 292, "metadata": { "tags": [] }, "outputs": [], "source": [ "s.map({'A':'A+','B':'B0'})" ], "id": "4e939093-2806-4da8-be48-4f99909a6a0d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "## B. `df.map()` = `df.applymap()`\n", "\n", "> **코랩실습자를 위한 주의사항1**\n", ">\n", "> 코랩에서 실습할경우\n", ">\n", "> ``` python\n", "> df.map()\n", "> ```\n", ">\n", "> 이 동작하지 않습니다. 대신 아래와 같이 `applymap`이 동작합니다.\n", ">\n", "> ``` python\n", "> df.applymap()\n", "> ```\n", ">\n", "> 이것은 코랩에서 기본으로 설치되어있는 pandas의 버전이 너무 낮아서\n", "> 생기는 문제입니다. 따라서 코랩을 쓰시는 분들은 아래의 강의노트들을\n", "> `df.applymap()`으로 바꿔서 실습하시기 바랍니다.\n", "\n", "> **코랩실습자를 위한 주의사항2**\n", ">\n", "> 만약 코랩에서도 `df.map()`을 사용하시려면 pandas를 높은버전으로 새로\n", "> 설치하고 사용하시면 됩니다. 즉\n", ">\n", "> 1. 코랩커널재시작 (컴퓨터 다시 할당)\n", "> 2. `import pandas as pd` 를 하기 전에 `!pip install pandas -U`를 이용하여 판다스를 최신버전으로 재설치 \n", "> 3. 판다스를 임포트 하고 (`import pandas as pd`) 실습\n", ">\n", "> 와 같이 하시면 됩니다.\n", "\n", "`-` 가능한 형태는 아래와 같다.\n", "\n", "1. 변환함수(스칼라입력,스칼라출력): 로그, 제곱\n", "2. 집계함수(벡터입력,스칼라출력)\n", "\n", "1만 쓸모있다. 여기에서 `df.map(변환함수)` 꼴은 사실\n", "`df.applymap(변환함수)`와 가능이 같다.\n", "\n", "`# 예제1` – `df.map` + 스칼라입력,스칼라출력" ], "id": "3d791d4e-a9cf-4469-b825-2e2d0182950b" }, { "cell_type": "code", "execution_count": 313, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'A':[2143,2143],'B':['-',3456]})\n", "df" ], "id": "6bb7d5c8-022b-47f1-8dec-c891b4a5da20" }, { "cell_type": "code", "execution_count": 314, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.map(lambda x: 0 if x == '-' else x)" ], "id": "b2717f07-aab3-48c4-99d3-040814e9d65e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제2` – `df.map` + 벡터입력,벡터출력 // 불가능해" ], "id": "aacddbb0-096b-431a-a1e4-3c4b54154597" }, { "cell_type": "code", "execution_count": 293, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'A':np.random.randn(5), 'B':np.random.randn(5)+5})\n", "df" ], "id": "e8cc61df-3581-4e0e-9228-2edcb836b5be" }, { "cell_type": "code", "execution_count": 297, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.map(np.sort) # 불가능.." ], "id": "94b4082b-74ae-4324-a421-e558b55bae53" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제3` – `df.map` + 벡터입력,스칼라출력(집계함수) // 가능하긴한데\n", "사실 스칼라입력,스칼라출력으로 해석해야함" ], "id": "dc334693-2467-42d4-bad3-0d528b8c5baa" }, { "cell_type": "code", "execution_count": 300, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'A':np.random.randn(5), 'B':np.random.randn(5)+5})\n", "df" ], "id": "9fd2ee9a-31e4-4722-8555-e8f7b474ec88" }, { "cell_type": "code", "execution_count": 303, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.map(np.mean) # 사실상 스칼라입력,스칼라출력으로 봐야함" ], "id": "9f26d949-2712-4268-bf04-9a1c4221d57c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예제4` – `df.map` + 딕셔너리 // 불가능" ], "id": "c1780334-fd58-47bc-b993-2a545e6210f4" }, { "cell_type": "code", "execution_count": 307, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'guebin':[0,1,0,1,0,1],'hynn':[0,1,1,1,1,1]})\n", "df" ], "id": "063f1149-77cc-4c5c-8bf6-f62f4e8d5c71" }, { "cell_type": "code", "execution_count": 310, "metadata": { "tags": [] }, "outputs": [], "source": [ "df.map({0:'fail',1:'pass'})" ], "id": "a1809966-7aa8-4c5a-a8a6-6d094928db83" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "# 5. HW\n", "\n", "아래의 자료가 있다고 하자." ], "id": "b5ec2132-78bd-4f3d-9089-657d55f97ae0" }, { "cell_type": "code", "execution_count": 437, "metadata": { "tags": [] }, "outputs": [], "source": [ "df = pd.DataFrame({'guebin':[0,1,0,1,0,1],'hynn':[0,1,1,1,1,1]})\n", "df" ], "id": "933c70fc-265d-4b74-a29f-0b5391b68e03" }, { "cell_type": "markdown", "metadata": {}, "source": [ "이 자료를 `lambda`와 `df.applymap`을 이용하여 아래와 같이 변경하라." ], "id": "af20dc16-91b5-4bba-8546-1aed98c6c3d4" }, { "cell_type": "code", "execution_count": 439, "metadata": { "tags": [] }, "outputs": [], "source": [ "#" ], "id": "a8d8d709-54b3-4c1f-8797-601e67b94ec8" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3 (ipykernel)", "language": "python" }, "language_info": { "name": "python", "codemirror_mode": { "name": "ipython", "version": "3" }, "file_extension": ".py", "mimetype": "text/x-python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } } }