如果想要应用自定义的函数,或者把其他库中的函数应用到 Pandas 对象中,有以下三种方法:
1 pipe
import pandas as pd
import numpy as np
#自定义函数
def adder(ele1,ele2):
return ele1+ele2
#操作DataFrame
df = pd.DataFrame(np.random.randn(4,3),columns=['c1','c2','c3'])
#相加前
print(df)
#相加后
print(df.pipe(adder,5))
结果:
c1 c2 c3
0 1.908222 -0.377214 -0.967820
1 -0.386066 0.298205 1.143535
2 -1.469431 0.287798 -1.371165
3 -0.724862 0.821063 -0.625724
c1 c2 c3
0 6.908222 4.622786 4.032180
1 4.613934 5.298205 6.143535
2 3.530569 5.287798 3.628835
3 4.275138 5.821063 4.374276
2 apply()
按行处理
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print(df)
print (df.apply(np.mean,axis=1))
0 -0.865303 0.380977 -1.371948
1 -0.735269 0.552093 -0.101040
2 0.329665 -0.710652 -1.206729
3 -0.480575 0.156047 1.099046
4 0.466110 0.295210 -0.419494
0 -0.618758
1 -0.094739
2 -0.529239
3 0.258173
4 0.113942
dtype: float64
默认按列处理
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print(df)
print(df.apply(lambda x: x.max() - x.min()))
结果
col1 col2 col3
0 -0.909495 -2.009906 1.532436
1 -0.276285 1.607327 -0.394461
2 0.490701 0.185447 -0.320529
3 0.738601 1.384119 -0.215178
4 0.933595 0.217680 -0.545816
col1 1.843090
col2 3.617233
col3 2.078252
dtype: float64
applymap
import pandas as pd
import numpy as np
#自定义函数
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print(df.applymap(lambda x:x*10))
print(df.apply(np.mean))
结果
col1 col2 col3
0 3.826257 -6.184871 -10.162795
1 -20.665111 1.466812 1.266041
2 -3.371027 18.351201 -4.044751
3 -10.797481 -21.777112 1.862200
4 5.660287 -17.092084 11.905230
col1 -0.506941
col2 -0.504721
col3 0.016518
dtype: float64