Pandas with Python 2.7 Part 10 - Function Mapping
Key Takeaways
This video covers function mapping in Pandas, allowing users to create custom functions and apply them to Pandas dataframes. The video demonstrates how to define a simple function, pass a column through it, and map the function to the dataframe using the map function.
Full Transcript
what's going on everybody welcome to another Python and pandas tutorial video in this video we're gonna be talking about is function mapping so the idea of this is you know with pandas get a lot of really great built-in operations that we can do um but we might come to a point where we find that there's not either either either isn't a built-in operation or we don't know it with pins and so this is where function mapping comes into play and function mapping still works pretty well with pandas so that's what I'm gonna be showing you guys today so the first thing I want to do is go ahead and just delete everything after the definition of DF I'm just really no point to have the rest of this stuff here so but delete that I'll make some more space in our edit code so now it's defined just a simple function this is gonna be a stupid simple function just to show you guys that first of all it will be row by row it will still iterate through the row or row by row rather and trying to figure out how to say this it basically will work as if like you would basically just build the function as if you were building it or as if it was a function built into pandas so let me just show you guys an example so let's say define function and then this function is all just gonna have some data that's passed through it so now we're gonna say x equals and we got import random let's just import random here in for random so X is equal gonna equal random not Rand range it's gonna be a random range 0 to 5 and then we're gonna just say return return data times X Wow there we go 2 times X and so what we're gonna do is we're going to pass a single column through here and the the data parameter is gonna be that whole column we're just gonna use it we're gonna pass through that pandas column and then X is equal a random 0 to 5 and what you'll find is that it for every row it's a random 0 to 5 multiple okay so this is how we know it's iterating through all the rows and instead of data we could pass through all of the columns or three columns you can you could put data data to data 3 and or column 1 column 2 column 3 whatever you want to call the parameters and you can perform functions on those parameters so for now we'll just do one column one parameter and it's just gonna be a random range just to kind of prove to you that it's really is iterating and doing random things per row and so anyway we'll see that in a second so now we're gonna say DF and then we're gonna say multiple because it's a multiple do you have got multiple equals and then we say map and then this parts a little tricky but it's not too fancy now we're going to say that the function that we want to map and that's gonna be a machine and then instead of putting in you know more parentheses here for the parameters instead what we do is we say function comma and then we put the parameters in and this one only has one parameter so we're just gonna put that one parameter but that parameter is DF close now what we do is print D F dot head save and run it and now we can see here's our multiple column right here and you can see here that we you know one of them looks like it multiplied it by two the next one it was zero next one is probably three or four and then in the next one the same thing again and then it's zero so as you can see already just with these few we definitely had different ones so it's definitely contingent by row and then it does the entire function itself by row so really quite simple to make your own function so obviously you could do all kinds of things yourself now with this and you can also pass through things like you could say you can reference previous data points and all that with with going through function mapping so there's this pretty much opens the door for anything that is not in pandas yet you want to kind of use the efficiency of pandas because this is gonna in rate through kind of like in generator form instead of a typical four loop that you might write so it's still gonna be pretty efficient it may not be the most efficient code since you know what you're writing in here might not be it very efficient but you'll still be able to utilize pandas and then once you're done it's still in the pandas dataframe and you can still do everything else that you've always done in pandas so that's just like a quick example of what you need to do to map a function so you can kind of keep working with pandas it's just in case pandas that have an operation that you really need you can write it and then map it so anyways that's it with this video if you guys have any questions or comments leave them below as always thanks for watching
Original Description
In this video we cover the notion of Function Mapping. Function mapping is not restricted to Pandas only. It is actually something that you can use in many cases. Function mapping, however, in the case of Pandas, will allow us to create our very own functions and pass them through Pandas as if it was a Part of Pandas. The downside here is that your efficiency in processing will be limited to how efficient you have written your code. If there is a way to do it in Pandas, you should try to do it with a tool they have provided to maintain speed, but, you can always use this method as a last resort to do exactly what you need.
Sample code for the series: http://pythonprogramming.net/python-2-7-pandas-data-analysis/
Pandas tutorial series: https://www.youtube.com/playlist?list=PLQVvvaa0QuDfHt4XU7vTm22xDegR0v0fQ
http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex
Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
Related AI Lessons
⚡
⚡
⚡
⚡
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
Why I built a simple AI provider wrapper (and you might too)
Dev.to · zhongqiyue
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · AI
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · ChatGPT
🎓
Tutor Explanation
DeepCamp AI