课程: Hands-On Advanced Python: Data Engineering Basics
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Using the reduce function - Python教程
课程: Hands-On Advanced Python: Data Engineering Basics
Using the reduce function
- [Instructor] Sometimes when you're working with a large dataset you want to be able to reduce that dataset down to a single value. And we've already seen some use cases for that right here in this course. So for example, the min and max functions, essentially, just reduce the entire dataset down to a single minimum or maximum value. So for example, we used min and max to find the coldest and warmest days out of the entire dataset. Python provides a more generic function called "Reduce" that does the same thing using a callback function that your code provides in order to control the reduction algorithm. So let's start with a simple example to see how this works. Let's go ahead and open up the reduce func example. And what I'm going to do first, is import the reduce function and that's in the func tools module. So I'm going to write from functools, import reduce. So let's use this reduce function to calculate how…