pandas 学习汇总13 - 函数应用- 将自定义或其他库函数应用于Pandas对象( tcy)
发布日期:2021-06-29 14:48:06 浏览次数:2 分类:技术文章

本文共 2115 字,大约阅读时间需要 7 分钟。

Pandas函数应用- 将自定义或其他库函数应用于Pandas对象(pipe,apply,applymap,map,agg) 2018/12/5  

1.函数:   

# 表函数应用:df.pipe(func, *args, **kwargs) #数据为Series,DataFrames或GroupBy对象s.pipe( func, *args, **kwargs)# 行或列函数应用:df.apply(func, axis=0, raw=False, result_type=None, args=(), **kwds)s.apply(func, convert_dtype=True, args=(), **kwds)# 参数:# raw =False输入为序列;=True输入为ndarray# result_type: {'expand', 'reduce', 'broadcast', None}在axis=1时起作用# 分别返回list,Series,原形状,自动推断# 元素函数应用:df.applymap(func)s.map(arg, na_action=None) #对应的序列的映射值(dict、Series或函数)# 聚合:s.agg(func, axis=0, *args, **kwargs)#使用指定轴上的一个或多个操作进行聚合 

2.pipe函数   

实例1:
def adder(x1,x2):   return x1+x2    s=pd.Series([1,2,3,4])df = pd.DataFrame([[1,2,3],[4,5,6]],columns=['col1','col2','col3'])    s.pipe(adder,2)# 0    3# 1    4# 2    5# 3    6# dtype: int64    df.pipe(adder,2)    #    col1  col2  col3# 0     3     4     5# 1     6     7     8

3.apply函数   

# 实例1:Series序列应用 def adder(x,y):return np.add(x,y)s.apply(lambda x:x+2) #lambda函数s.apply(adder,args=(2,)) #自定义函数def sum(x, **kwargs):for month in kwargs:x+=kwargs[month]return xs.apply(sum,a1=1,a2=2,a3=3)#多参数s.apply(np.log) #库函数# 0 0.000000# 1 0.693147# 2 1.098612# 3 1.386294# dtype: float64 

实例2:DataFrame应用: 

# 用apply()方法沿DataFrame或Panel的轴应用任意函数df.apply(np.mean)#默认axis=0,在列上计算# col1 2.5# col2 3.5# col3 4.5# dtype: float64df.apply(np.mean,axis=1)# 0 2.0# 1 5.0 

4.映射  

# 实例1:Series# 并不是所有的函数都可以向量化df['col1'].map(lambda x:x*2)s.map(lambda x:x*2)x = pd.Series([1,2,3], index=['a1', 'a2', 'a3'])y = pd.Series(['v1', 'v2', 'v3'], index=[1,2,3])x.map(y)#映射序列# a1 v1# a2 v2# a3 v3# dtype: objectz = {1: 'A', 2: 'B', 3: 'C'}x.map(z)#映射字典# a1 A# a2 B# a3 C# dtype: objects = pd.Series([1, 2, 3, np.nan])s2 = s.map('str = {}'.format, na_action=None)#Na值# 0 str = 1.0# 1 str = 2.0# 2 str = 3.0# 3 str = nan# dtype: objects3 = s.map('str = {}'.format, na_action='ignore')# 0 str = 1.0# 1 str = 2.0# 2 str = 3.0# 3 NaN# dtype: object 

实例2:DataFrame 

df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])df.applymap(lambda x:x*2)format = lambda x: "%.2f" % xdf.applymap(format)# col1 col2 col3# 0 1.00 2.00 3.00# 1 4.00 5.00 6.00 

 

转载地址:https://chunyou.blog.csdn.net/article/details/84804384 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:pandas 14 - 重新索引reindex,reindex_like,rename( tcy)
下一篇:pandas 学习汇总12 - 描述性统计(比较全 tcy)

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月13日 05时50分07秒