import pandas as pd import numpy as np df=pd.DataFrame(np.random.randn(4,3),index=['utah','ohio','texas','ny'],columns=list('abc')) help(df.apply) # Objects passed to functions are series objects having index either the DataFrames index (axis=0) 每列来,or the columns(axis=1) # 按每行来. Return type depends on whether passed function aggregates, or the reduce argument if the DataFrame is empty. # Parameters # func:function # Function to apply to each column/row # axis:{0 or 'index',1 or 'columns'},default 0 # * 0 or 'index':apply function to each column (0 竖) # * 1 or 'columns':apply function to each row (1 横) ##apply function接收的是一个带index的Series,axis=0则竖Series,即每列,axis=1为每行 f = lambda x: x.max() - x.min() print (df.apply(f,axis=1)) # applymap # applymap对一个DataFrame中的每个元素进行转换 format=lambda x: '%.2f' %x print(df.applymap(format)) # map()只要是作用将函数作用于一个Series的每一个元素,用法如下所示 print(df['b'].map(format)) # 总的来说就是apply()是一种让函数作用于列或者行操作, # applymap()是一种让函数作用于DataFrame每一个元素的操作,而map是一种让函数作用于Series每一个元素的操作。
pandas map apply applymap区别
最新推荐文章于 2025-02-19 07:30:00 发布