DataFrame的apply()、applymap()、map()方法[通俗易懂]

DataFrame的apply()、applymap()、map()方法[通俗易懂]对DataFrame对象中的某些行或列,或者对DataFrame对象中的所有元素进行某种运算或操作,我们无需利用低效笨拙的循环,DataFrame给我们分别提供了相应的直接而简单的方法,apply()和applymap()。其中apply()方法是针对某些行或列进行操作的,而applymap()方法则是针对所有元素进行操作的。1map()方法Themapmethod…

大家好,又见面了,我是你们的朋友全栈君。

       对DataFrame对象中的某些行或列,或者对DataFrame对象中的所有元素进行某种运算或操作,我们无需利用低效笨拙的循环,DataFrame给我们分别提供了相应的直接而简单的方法,apply()和applymap()。其中apply()方法是针对某些行或列进行操作的,而applymap()方法则是针对所有元素进行操作的。

 1 map()方法

The map method works on series, so in our case, we will use it to transform a column of our DataFrame, which remember is just a pandas Series. Suppose that we decide that the class names are a bit long for our taste and we would like to code them using our special threeletter coding system. We’ll use the map method with a Python dictionary as the argument toaccomplish this. We’ll pass in a replacement for each of the unique iris types:

df[‘class’] = df[‘class’].map({‘Iris-setosa’: ‘SET’, ‘Iris-virginica’:’VIR’, ‘Iris-versicolor’: ‘VER’})
df

DataFrame的apply()、applymap()、map()方法[通俗易懂]

2 Apply()方法

The apply method allows us to work with both DataFrames and Series. We’ll start with an example that would work equally well with map, then we’ll move on to examples that would work only with apply.

Using the iris DataFrame, let’s make a new column based on the petal width. We previously saw that the mean for the petal width was 1.3. Let’s now create a new column in our DataFrame, wide petal, that contains binary values based on the value in the petal width column. If the petal width is equal to or wider than the median, we will code it with a 1, and if it is less than the median, we will code it 0. We’ll do this using the apply method on the petal width column:

df[‘wide petal’] = df[‘petal width’].apply(lambda v: 1 if v >= 1.3 else 0)
df

DataFrame的apply()、applymap()、map()方法[通俗易懂]

df[‘petal area’] = df.apply(lambda r: r[‘petal length’] * r[‘petal width’],axis=1)
df

DataFrame的apply()、applymap()、map()方法[通俗易懂]

3 Applymap()方法

We’ve looked at manipulating columns and explained how to work with rows, but suppose that you’d like to perform a function across all data cells in your DataFrame; this is where applymap is the right tool. Let’s take a look at an example:

df.applymap(lambda v: np.log(v) if isinstance(v, float) else v)

DataFrame的apply()、applymap()、map()方法[通俗易懂]

4 Groupby方法

df.groupby(‘class’).mean()

df.groupby(‘petalwidth’)[‘class’].unique().to_frame()

df.groupby(‘petalwidth’)[‘class’].unique().to_frame()

DataFrame的apply()、applymap()、map()方法[通俗易懂]

df.groupby(‘petal width’)[‘class’].unique().to_frame()

df.groupby(‘class’).describe()

df.groupby(‘class’)[‘petal width’].agg({‘delta’: lambda x: x.max() – x.min(), ‘max’: np.max, ‘min’: np.min})

   

简单来说,apply()方法 可以作用于DataFrame 还有Series, 作用于一行或者一列时,我们不妨可以采用,因为可以通过设置axis=0/1 来把握,demo如下:

DataFrame的apply()、applymap()、map()方法[通俗易懂]

applymap() 作用于每一个元素

DataFrame的apply()、applymap()、map()方法[通俗易懂]

map可以作用于Series每一个元素的

DataFrame的apply()、applymap()、map()方法[通俗易懂]

总的来说,map()、aply()、applymap()方法是一种对series、dataframe极其方便的应用与映射函数。

最后,非常重要的一点,这些映射函数,里面都是可以放入自定义函数的。

tips.head()

Out[34]:

total_bill tip smoker day time size tip_pct
0 16.99 1.01 No Sun Dinner 2 0.059447
1 10.34 1.66 No Sun Dinner 3 0.160542
2 21.01 3.50 No Sun Dinner 3 0.166587
3 23.68 3.31 No Sun Dinner 2 0.139780
4 24.59 3.61 No Sun Dinner 4 0.146808

def top(df,n=5,column=’tip_pct’):
    return df.sort_values(by=column)[-n:]

tips.groupby(‘smoker’).apply(top)

Out[38]:

total_bill tip smoker day time size tip_pct
smoker
No 88 24.71 5.85 No Thur Lunch 2 0.236746
185 20.69 5.00 No Sun Dinner 5 0.241663
51 10.29 2.60 No Sun Dinner 2 0.252672
149 7.51 2.00 No Thur Lunch 2 0.266312
232 11.61 3.39 No Sat Dinner 2 0.291990
Yes 109 14.31 4.00 Yes Sat Dinner 2 0.279525
183 23.17 6.50 Yes Sun Dinner 4 0.280535
67 3.07 1.00 Yes Sat Dinner 1 0.325733
178 9.60 4.00 Yes Sun Dinner 2 0.416667
172 7.25 5.15 Yes Sun Dinner 2 0.710345
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/145590.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月17日 上午7:00
下一篇 2022年5月17日 上午7:20


相关推荐

  • nvidia显示设置不可用_纯干货!手把手教你为FreeSync显示器开启G-Sync教程

    nvidia显示设置不可用_纯干货!手把手教你为FreeSync显示器开启G-Sync教程就在刚刚过去的CES2019年上,英伟达为我们带来了G-SyncCompatible,兼容VESAAdaptive-Sync和AMDFreesync显示器,不久前最新的驱动也已经发布,现在就可以为你的显示器开启G-SyncCompatible了。众所周知,NVIDIA的游戏显卡备受玩家推崇,在此之前,G-SYNC技术只能针对G-SYNC显示器进行搭配。兼容技术的到来,意味着使用NVIDI…

    2022年5月2日
    139
  • Weblogic入门

    Weblogic入门Weblogic

    2026年3月17日
    2
  • sntp服务器位置,sntp的服务器地址

    sntp服务器位置,sntp的服务器地址sntp的服务器地址内容精选换一换介绍常见的安全组配置示例。如下示例中,出方向默认全通,仅介绍入方向规则配置方法。不同安全组内的云耀云服务器内网互通仅允许特定IP地址远程连接云耀云服务器SSH远程连接Linux云耀云服务器RDP远程连接Windows云服务器公网ping云耀云服务器云耀云服务器作Web服务器云耀云服务器作DNS服务器使用FTP上传或下载文件场景举例:在介绍常见的安全组配置示例…

    2025年7月15日
    4
  • MAC下最好用的抓包工具–charles简单操作教程

    MAC下最好用的抓包工具–charles简单操作教程一、Charles介绍给大家推荐一款在mac上我觉得很好用的抓包工具,再过去的半年中给我很大帮助,在工作学习中使用很方便。那么什情况下我们会需要使用抓包工具呢,比如我想查看一个接口请求的参数、返回值,还有移动设备上的http请求、https请求,有了charles一下搞定,妈妈再也不用担心我的学习了,咳咳……,回归正题,介绍一下charles。Charles是一个HTTP代理服务器…

    2022年6月12日
    62
  • 数位dp总结 之 从入门到模板

    数位dp总结 之 从入门到模板for inti le i ri i if right i ans 基础篇数位 dp 是一种计数用的 dp 一般就是要统计一个区间 le ri 内满足一些条件数的个数 所谓数位 dp 字面意思就是在数位上进行 dp 咯 数位还算是比较好听的名字 数位的含义 一个数有个位 十位 百位 千位 数的每一位就是数位啦 之所以要引入数位的概念完全就是为了 dp

    2026年3月20日
    2
  • git从gitlab拉取最新的代码到本地

    git从gitlab拉取最新的代码到本地1 Git 下载地址 https www git scm com download win2 安装 Git 安装步骤参考 https www cnblogs com xwgcxk p 9209237 html 3 从管理员手中拿到用户名 邮箱 密码假设用户名为 itcats cn 邮箱为 itcats cn itcats cn 密码为 123456784 初始化 git 信息

    2026年3月26日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号