{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mapping&Replace" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 20, "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", "
城市人口
A北京1000
B上海2000
C广州1500
\n", "
" ], "text/plain": [ " 城市 人口\n", "A 北京 1000\n", "B 上海 2000\n", "C 广州 1500" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1 = pd.DataFrame({'城市': ['北京', '上海', '广州'], '人口': [1000, 2000, 1500]}, index=['A', 'B', 'C'])\n", "df1" ] }, { "cell_type": "code", "execution_count": 21, "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", "
城市人口GDP
A北京1000NaN
B上海2000NaN
C广州1500NaN
\n", "
" ], "text/plain": [ " 城市 人口 GDP\n", "A 北京 1000 NaN\n", "B 上海 2000 NaN\n", "C 广州 1500 NaN" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1['GDP'] = pd.Series([1000, 2000, 1500])\n", "df1" ] }, { "cell_type": "code", "execution_count": 22, "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", "
城市人口GDP
A北京10001100
B上海20002100
C广州15001600
\n", "
" ], "text/plain": [ " 城市 人口 GDP\n", "A 北京 1000 1100\n", "B 上海 2000 2100\n", "C 广州 1500 1600" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gdp_map = {'北京': 1100, '上海': 2100, '广州': 1600}\n", "df1['GDP'] = df1['城市'].map(gdp_map)\n", "df1" ] }, { "cell_type": "code", "execution_count": 23, "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", "
城市人口GDP
A北京10001000
B上海20002000
C广州15001500
\n", "
" ], "text/plain": [ " 城市 人口 GDP\n", "A 北京 1000 1000\n", "B 上海 2000 2000\n", "C 广州 1500 1500" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df1['GDP'] = pd.Series([1000, 2000, 1500], index=['A', 'B', 'C'])\n", "df1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Replace in Series" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 0\n", "1 1\n", "2 2\n", "3 3\n", "4 4\n", "dtype: int64" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 = pd.Series(np.arange(5))\n", "s1" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 0.0\n", "1 NaN\n", "2 2.0\n", "3 3.0\n", "4 4.0\n", "dtype: float64" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.replace(1, np.nan)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 0\n", "1 10\n", "2 20\n", "3 30\n", "4 4\n", "dtype: int64" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.replace([1, 2, 3], [10, 20, 30])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }