官术网_书友最值得收藏!

map

We'll now begin with the map function. The map function works on series, so in our case we will use it to transform a column of our DataFrame, which you will recall is just a pandas series. Suppose we decide that the species numbers are not suitable for our needs. We'll use the map function with a Python dictionary as the argument to accomplish this. We'll pass in a replacement for each of the unique iris types:

Let's look at what we have done here. We have run the map function over each of the values of the existing species column. As each value was found in the Python dictionary, it was added to the return series. We assigned this return series to the same species name, so it replaced our original species column. Had we chosen a different name, say short code, that column would have been appended to the DataFrame, and we would then have the original species column plus the new short code column.

We could have instead passed the map function a series or a function to perform this transformation on a column, but this is a functionality that is also available through the apply function, which we'll take a look at next. The dictionary functionality is unique to the map function, and the most common reason to choose map over apply for a single column transformation. But, let's now take a look at the apply function.

主站蜘蛛池模板: 济源市| 龙泉市| 三都| 绍兴市| 金华市| 卢龙县| 武邑县| 乌兰浩特市| 洪江市| 汝阳县| 道孚县| 黄大仙区| 大关县| 曲周县| 闵行区| 宁城县| 新源县| 灵璧县| 皋兰县| 商丘市| 朝阳县| 盐源县| 鹿泉市| 灵川县| 星子县| 故城县| 信丰县| 神木县| 都安| 泗水县| 荣昌县| 丹阳市| 新郑市| 汶上县| 五华县| 隆化县| 静海县| 古蔺县| 洛隆县| 博湖县| 深泽县|