Python apply函数运用

编程入门 行业动态 更新时间:2024-10-25 11:22:04

Python apply<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数运用"/>

Python apply函数运用

1、介绍
apply函数是pandas里面所有函数中自由度最高的函数。该函数如下:

DataFrame.apply(func, axis=0, broadcast=False, raw=False, reduce=None, args=(), **kwds)

该函数最有用的是第一个参数,这个参数是函数,相当于C/C++的函数指针。

这个函数需要自己实现,函数的传入参数根据axis来定,比如axis = 1,就会把一行数据作为Series的数据 结构传入给自己实现的函数中,我们在函数中实现对Series不同属性之间的计算,返回一个结果,则apply函数 会自动遍历每一行DataFrame的数据,最后将所有结果组合成一个Series数据结构并返回。2、样例

import numpy as np
import pandas as pdf = lambda x: x.max()-x.min()df = pd.DataFrame(np.random.randn(4,3),columns=list('bde'),index=['utah', 'ohio', 'texas', 'oregon'])
print(df)t1 = df.apply(f)
print(t1)t2 = df.apply(f, axis=1)
print(t2)#输出结果如下所示:b         d         e
utah    1.106486  0.101113 -0.494279
ohio    0.955676 -1.889499  0.522151
texas   1.891144 -0.670588  0.106530
oregon -0.062372  0.991231  0.294464b    1.953516
d    2.880730
e    1.016430
dtype: float64utah      1.600766
ohio      2.845175
texas     2.561732
oregon    1.053603
dtype: float64


3、性能比较

df = pd.DataFrame({'a': np.random.randn(6),'b': ['foo', 'bar'] * 3,'c': np.random.randn(6)})def my_test(a, b):return a + bprint(df)df['Value'] = df.apply(lambda row: my_test(row['a'], row['c']), axis=1) # 方法1
print(df)df['Value2'] = df['a'] + df['c']  # 方法2
print(df)#输出结果如下:a    b         c
0 -1.194841  foo  1.648214
1 -0.377554  bar  0.496678
2  1.524940  foo -1.245333
3 -0.248150  bar  1.526515
4  0.283395  foo  1.282233
5  0.117674  bar -0.094462a    b         c     Value
0 -1.194841  foo  1.648214  0.453374
1 -0.377554  bar  0.496678  0.119124
2  1.524940  foo -1.245333  0.279607
3 -0.248150  bar  1.526515  1.278365
4  0.283395  foo  1.282233  1.565628
5  0.117674  bar -0.094462  0.023212a    b         c     Value    Value2
0 -1.194841  foo  1.648214  0.453374  0.453374
1 -0.377554  bar  0.496678  0.119124  0.119124
2  1.524940  foo -1.245333  0.279607  0.279607
3 -0.248150  bar  1.526515  1.278365  1.278365
4  0.283395  foo  1.282233  1.565628  1.565628
5  0.117674  bar -0.094462  0.023212  0.023212

注意:当数据量很大时,对于简单的逻辑处理建议方法2(个人处理几百M数据集时,方法1花时200s左右,方法2花时10s)!!!
————————————————
版权声明:本文为CSDN博主「鸿燕藏锋」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:

更多推荐

Python apply函数运用

本文发布于:2024-03-23 23:16:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1744006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Python   apply

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!