Skip to content

Instantly share code, notes, and snippets.

@hygull
Last active November 19, 2018 06:15
Show Gist options
  • Save hygull/2940ac8904088691b9e16d1a3a24c9cc to your computer and use it in GitHub Desktop.
Save hygull/2940ac8904088691b9e16d1a3a24c9cc to your computer and use it in GitHub Desktop.
Lambda functions in pandas
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# LAMBDA FUNCTION\n",
"# - Keyword: lambda\n",
"# - Used to define tiny anonymous function\n",
"# - It can/cannot take parameters and return some result\n",
"# - Syntax: \n",
"# lambda [args]: statements\n",
"# lambda [args]: return value "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Tiny anonymous functions\n",
"def add(a, b, *args, **kwargs):\n",
" print('args: ', args) # tuple\n",
" print('kwargs', kwargs) # dictionary\n",
" return a ** 2 + b ** 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"args: (34, 567)\n",
"kwargs {'k': 8, 'p': 9, 'l': 67}\n"
]
},
{
"data": {
"text/plain": [
"125"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = add(10, 5, 34, 567, k=8, p=9, l=67)\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#**************************** ADD ******************************************"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<function __main__.<lambda>(a, b, c)>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# LAMBDA\n",
"# Let's do the same using lambda function\n",
"lambda a, b, c: a ** 2 + c # a function which takes 2 parameters and returns a"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# LAMBDA\n",
"# Storing it in a variable\n",
"\n",
"add2 = lambda a2, b2: a2 ** b2 # func = lambda [parameters]: return_value , func([parameter with values])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"function"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(add2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"64"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Calling the lambda function\n",
"\n",
"c3 = add2(4, 3)\n",
"c3"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"#************************* EVEN/ODD *********************************************"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Define a lambda function \n",
"# which takes 1 int paramers and decides if it is an \n",
"# even/odd number\n",
"\n",
"is_even = lambda num: \"EVEN\" if num % 2 == 0 else \"ODD\" # ODD / EVEN"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'EVEN'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_even(34)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"f = (lambda a, b: a + b + 1)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(10, 5)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ODD'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_even(45)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'EVEN'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_even(-4)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"#**************************** SORTED ******************************************"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"# One important use of lambda while sorting using sorted function\n",
"list1 = [10, 3, 1, 2, 4, 9, 6, 7, 5, 8]\n",
"list2 = ['10', 3, 1, '2', 4, 9, '6', 7, 5, '8']"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted(list1)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"# sorted(list2) # Results an ERROR"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, '2', 3, 4, 5, '6', 7, '8', 9, '10']"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#sorted(lst, key = lambda_func) - FIXING THE ERROR\n",
"\n",
"sorted(list2, key = lambda n: int(n))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"#**************************** map() & lambda - Getting squares of numbers in a list ******************************************"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"l = [1, 2, 3, 4, 5, 6, 7]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"func = lambda n: n**2\n",
"squared_l = map(func, l) # one by one it processes the items in a list and place the result in a new list\n",
"# squared_l"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 4, 9, 16, 25, 36, 49]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(squared_l) # map object to list"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"#**************************** filter() & lambda - Filtering all even numbers ******************************************\n",
"# lambda returns True/False"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"l2 = [24, 45, 22, 41, 0, 20, 11, 12]\n",
"func2 = lambda n: True if n % 2 == 0 else False"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<filter at 0x19cc1fc97b8>"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"evens = filter(func2, l2)\n",
"evens"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[24, 22, 0, 20, 12]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(evens) # filter object => list"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"#**************************** reduce() & lambda - Finding sum of all the numbers in list *****************************\n",
"# lambda takes 2 parameters"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"l3 = [6, 4, 2, 1, 6, 1]\n",
"func3 = lambda a, b: a + b"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"total = reduce(func3, l3)\n",
"total"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"# String concatenation\n",
"words = [\"My \", \" name \", \"is \", \"Mike Robert\"]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'My name is Mike Robert'"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence = reduce(func3, words)\n",
"sentence"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"# IF ELIF ELSE example in lambda\n",
"def f(n):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"f2 = lambda n: \"EQUAL TO 30\" if n == 30 else (\"LESS THAN 30\" if n < 30 else \"GREATER THAN 30\")"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"# Let's use lambda with pandas"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>odd</th>\n",
" <th>even</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>11</td>\n",
" <td>42</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>45</td>\n",
" <td>24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>67</td>\n",
" <td>68</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>45</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>67</td>\n",
" <td>30</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" odd even\n",
"0 11 42\n",
"1 45 24\n",
"2 67 68\n",
"3 45 34\n",
"4 67 30"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame({'odd': [11, 45, 67, 45, 67], \"even\": [42, 24, 68, 34, 30]})\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"# Adding 1 to all columns of DataFrame\n",
"# apply() : https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html\n",
"\n",
"def f(x):\n",
" # print('x=', type(x), x)\n",
" return x + 1"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>col1</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>243</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>45</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>-6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>-2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>-7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>-3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>-2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" col1\n",
"0 243\n",
"1 45\n",
"2 -6\n",
"3 0\n",
"4 3\n",
"5 -2\n",
"6 5\n",
"7 -7\n",
"8 -3\n",
"9 -2"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2 = pd.DataFrame({'col1': [243, 45, -6, 0, 3, -2, 5, -7, -3, -2]})\n",
"df2"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"# df2.append(pd.to_datetime(pd.date_range('1-1-2018',periods=10).to_series()), ignore_index=True, axis=0) # FOCUS POINT ADD date_range()\n",
"dti = pd.date_range('1-1-2018',periods=10)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>col2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2018-01-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2018-01-02</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2018-01-03</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2018-01-04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2018-01-05</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>2018-01-06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>2018-01-07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>2018-01-08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>2018-01-09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>2018-01-10</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" col2\n",
"0 2018-01-01\n",
"1 2018-01-02\n",
"2 2018-01-03\n",
"3 2018-01-04\n",
"4 2018-01-05\n",
"5 2018-01-06\n",
"6 2018-01-07\n",
"7 2018-01-08\n",
"8 2018-01-09\n",
"9 2018-01-10"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df3 = pd.DataFrame(dti, columns=[\"col2\"])\n",
"df3"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>col1</th>\n",
" <th>col2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>243</td>\n",
" <td>2018-01-01</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>45</td>\n",
" <td>2018-01-02</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>-6</td>\n",
" <td>2018-01-03</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" <td>2018-01-04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>3</td>\n",
" <td>2018-01-05</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>-2</td>\n",
" <td>2018-01-06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>5</td>\n",
" <td>2018-01-07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>-7</td>\n",
" <td>2018-01-08</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>-3</td>\n",
" <td>2018-01-09</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>-2</td>\n",
" <td>2018-01-10</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" col1 col2\n",
"0 243 2018-01-01\n",
"1 45 2018-01-02\n",
"2 -6 2018-01-03\n",
"3 0 2018-01-04\n",
"4 3 2018-01-05\n",
"5 -2 2018-01-06\n",
"6 5 2018-01-07\n",
"7 -7 2018-01-08\n",
"8 -3 2018-01-09\n",
"9 -2 2018-01-10"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.concat([df2, df3], axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"# is_negative = False\n",
"# count = 0 \n",
"\n",
"# for i in df2['col1']:\n",
"# if i < 0:\n",
"# is_negative = True\n",
"# print(i)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"col1 5\n",
"dtype: int64"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.loc[df2.col1 < 0].count() # Finding number of consecutive + and - values sequence"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>odd</th>\n",
" <th>even</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>12</td>\n",
" <td>43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>46</td>\n",
" <td>25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>68</td>\n",
" <td>69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>46</td>\n",
" <td>35</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>68</td>\n",
" <td>31</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" odd even\n",
"0 12 43\n",
"1 46 25\n",
"2 68 69\n",
"3 46 35\n",
"4 68 31"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.apply(f) "
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"# Adding 2 to all the elemets in DataFrame\n",
"f2 = lambda y: y + 1"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>odd</th>\n",
" <th>even</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>12</td>\n",
" <td>43</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>46</td>\n",
" <td>25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>68</td>\n",
" <td>69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>46</td>\n",
" <td>35</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>68</td>\n",
" <td>31</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" odd even\n",
"0 12 43\n",
"1 46 25\n",
"2 68 69\n",
"3 46 35\n",
"4 68 31"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.apply(f2)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>odd</th>\n",
" <th>even</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>True</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" odd even\n",
"0 False True\n",
"1 True False\n",
"2 True True\n",
"3 True True\n",
"4 True False"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Getting rows with values > 30\n",
"df.apply(lambda x: x > 30)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>odd</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>45</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>67</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>45</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>67</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" odd\n",
"0 11\n",
"1 45\n",
"2 67\n",
"3 45\n",
"4 67"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[:,['odd']]"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>odd</th>\n",
" <th>even</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>45</td>\n",
" <td>24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>67</td>\n",
" <td>68</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>45</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>67</td>\n",
" <td>30</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" odd even\n",
"1 45 24\n",
"2 67 68\n",
"3 45 34\n",
"4 67 30"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[df.odd > 30]"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"# func1 = lambda a, b: a ** 2"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"# func2 = lambda a, b: b ** 3"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"def a(f1, f2): # f1 is a function which returns square, f2 function returns cube\n",
" return f1(10, 2) + f2(10, 3) "
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"value = a(lambda a, b: a ** 2 + b, lambda a, b: b ** 3 + a)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"139"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"value"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment