>>> from env_helper import info; info()
页面更新时间: 2023-12-16 21:44:01
运行环境:
Linux发行版本: Debian GNU/Linux 12 (bookworm)
操作系统内核: Linux-6.1.0-15-amd64-x86_64-with-glibc2.36
Python版本: 3.11.2
6.7. Pandas函数应用¶
要将自定义或其他库的函数应用于Pandas对象,有三个重要的方法,下面来讨论如何使用这些方法。使用适当的方法取决于函数是否期望在整个DataFrame,行或列或元素上进行操作。
表合理函数应用:pipe()
行或列函数应用:apply()
元素函数应用:applymap()
6.7.1. 表格函数应用¶
可以通过将函数和适当数量的参数作为管道参数来执行自定义操作。 因此,对整个DataFrame执行操作。
例如,为DataFrame中的所有元素相加一个值2。
adder 函数
adder函数将两个数值作为参数相加并返回总和。
def adder(ele1,ele2):
return ele1+ele2
现在将使用自定义函数对DataFrame进行操作。
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
df.pipe(adder,2)
下面来看看完整的程序 -
>>> import pandas as pd
>>> import numpy as np
>>>
>>> def adder(ele1,ele2):
>>> return ele1+ele2
>>>
>>> df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
>>> df.pipe(adder,2)
>>> print(df)
col1 col2 col3
0 -0.512746 -1.928741 0.088373
1 0.744521 -0.882974 -0.791463
2 -1.114707 1.368742 -2.500995
3 1.190780 0.798515 -0.305547
4 -1.063275 -0.302117 0.243225
6.7.2. 行或列合理函数应用¶
可以使用apply()方法沿DataFrame或Panel的轴应用任意函数,它与描述性统计方法一样,采用可选的axis参数。 默认情况下,操作按列执行,将每列列为数组。
示例¶
>>> df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
>>> df.apply(np.mean)
>>> print(df)
col1 col2 col3
0 0.875645 0.238500 -1.163282
1 1.041110 -1.617998 0.721465
2 -0.664245 0.170676 0.673088
3 0.214794 -0.617841 -0.544137
4 1.097139 -0.376302 1.657833
通过传递axis参数,可以在行上执行操作。
示例-2¶
>>> df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
>>> df.apply(np.mean,axis=1)
>>> df
col1 | col2 | col3 | |
---|---|---|---|
0 | -0.065411 | -0.597300 | -0.121994 |
1 | 0.717776 | -1.657757 | 0.303157 |
2 | -0.268773 | 1.087726 | -1.170008 |
3 | -1.334092 | -0.389287 | 0.916458 |
4 | 0.290341 | 0.442119 | 0.916656 |
示例-3¶
>>> df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
>>> df.apply(lambda x: x.max() - x.min())
>>> df
col1 | col2 | col3 | |
---|---|---|---|
0 | 1.504646 | -0.363806 | -0.681664 |
1 | -0.498563 | -0.062275 | -0.512961 |
2 | 0.286375 | -0.073808 | 0.921349 |
3 | -1.134467 | 1.990831 | -2.484318 |
4 | 0.242786 | 0.355586 | 0.154012 |
6.7.3. 元素合理函数应用¶
并不是所有的函数都可以向量化(也不是返回另一个数组的NumPy数组,也不是任何值),在DataFrame上的方法applymap()和类似于在Series上的map()接受任何Python函数,并且返回单个值。
示例-1¶
>>> df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
>>>
>>> # My custom function
>>> df['col1'].map(lambda x:x*100)
>>> df
col1 | col2 | col3 | |
---|---|---|---|
0 | 0.538401 | 0.885684 | 1.507353 |
1 | -0.494505 | -1.206135 | 1.115960 |
2 | 0.949553 | 0.678105 | -0.237772 |
3 | 0.290807 | -0.526670 | 0.661652 |
4 | -0.720237 | -0.460505 | -0.845880 |
示例-2¶
>>> # My custom function
>>> df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
>>> df.applymap(lambda x:x*100)
>>> df
col1 | col2 | col3 | |
---|---|---|---|
0 | 1.549599 | 1.684163 | -0.303522 |
1 | -1.844774 | -0.653872 | 0.959466 |
2 | -0.997209 | 1.290200 | 0.382495 |
3 | 2.710082 | 0.782055 | -0.397633 |
4 | 1.422971 | 0.031233 | -0.196813 |