- Python Pandas 教程
- Python Pandas - 首页
- Python Pandas - 简介
- Python Pandas - 环境搭建
- Python Pandas - 基础知识
- Python Pandas - 数据结构介绍
- Python Pandas - 索引对象
- Python Pandas - Panel (面板)
- Python Pandas - 基本功能
- Python Pandas - 索引和数据选择
- Python Pandas - Series (序列)
- Python Pandas - Series (序列)
- Python Pandas - Series 对象切片
- Python Pandas - Series 对象属性
- Python Pandas - Series 对象的算术运算
- Python Pandas - 将 Series 转换为其他对象
- Python Pandas - DataFrame (数据框)
- Python Pandas - DataFrame (数据框)
- Python Pandas - 访问 DataFrame
- Python Pandas - DataFrame 对象切片
- Python Pandas - 修改 DataFrame
- Python Pandas - 从 DataFrame 中删除行
- Python Pandas - DataFrame 的算术运算
- Python Pandas - I/O 工具
- Python Pandas - I/O 工具
- Python Pandas - 使用 CSV 格式
- Python Pandas - 读取和写入 JSON 文件
- Python Pandas - 从 Excel 文件读取数据
- Python Pandas - 将数据写入 Excel 文件
- Python Pandas - 使用 HTML 数据
- Python Pandas - 剪贴板
- Python Pandas - 使用 HDF5 格式
- Python Pandas - 与 SQL 的比较
- Python Pandas - 数据处理
- Python Pandas - 排序
- Python Pandas - 重新索引
- Python Pandas - 迭代
- Python Pandas - 级联
- Python Pandas - 统计函数
- Python Pandas - 描述性统计
- Python Pandas - 处理文本数据
- Python Pandas - 函数应用
- Python Pandas - 选项和自定义
- Python Pandas - 窗口函数
- Python Pandas - 聚合
- Python Pandas - 合并/连接
- Python Pandas - 多层索引
- Python Pandas - 多层索引基础
- Python Pandas - 使用多层索引进行索引
- Python Pandas - 使用多层索引的高级重新索引
- Python Pandas - 重命名多层索引标签
- Python Pandas - 对多层索引进行排序
- Python Pandas - 二元运算
- Python Pandas - 二元比较运算
- Python Pandas - 布尔索引
- Python Pandas - 布尔掩码
- Python Pandas - 数据重塑和透视
- Python Pandas - 透视表
- Python Pandas - 堆叠和取消堆叠
- Python Pandas - 熔化
- Python Pandas - 计算虚拟变量
- Python Pandas - 类别数据
- Python Pandas - 类别数据
- Python Pandas - 类别数据的排序和分类
- Python Pandas - 比较类别数据
- Python Pandas - 处理缺失数据
- Python Pandas - 缺失数据
- Python Pandas - 填充缺失数据
- Python Pandas - 缺失值的插值
- Python Pandas - 删除缺失数据
- Python Pandas - 缺失数据的计算
- Python Pandas - 处理重复项
- Python Pandas - 重复数据
- Python Pandas - 计数和检索唯一元素
- Python Pandas - 重复标签
- Python Pandas - 分组和聚合
- Python Pandas - GroupBy
- Python Pandas - 时间序列数据
- Python Pandas - 日期功能
- Python Pandas - Timedelta (时间差)
- Python Pandas - 稀疏数据结构
- Python Pandas - 稀疏数据
- Python Pandas - 数据可视化
- Python Pandas - 数据可视化
- Python Pandas - 其他概念
- Python Pandas - 注意事项和陷阱
- Python Pandas 有用资源
- Python Pandas - 快速指南
- Python Pandas - 有用资源
- Python Pandas - 讨论
Python Pandas - 函数应用
Pandas 提供了强大的方法来将自定义函数或库函数应用于 DataFrame 和 Series 对象。根据您是想将函数应用于整个 DataFrame、按行或按列,还是按元素应用,Pandas 提供了几种方法来实现这些任务。
在本教程中,我们将探讨 Pandas 中三种基本的函数应用方法:
- 表级函数应用:pipe()
- 行或列级函数应用:apply()
- 元素级函数应用:map()
让我们深入了解每种方法,看看如何有效地利用它们。
表级函数应用
pipe() 函数允许您应用可链接的函数,这些函数期望 DataFrame 或 Series 作为输入。此方法对于以清晰易读的方式对整个 DataFrame 执行自定义操作非常有用。
示例:将自定义函数应用于整个 DataFrame
以下示例演示了如何使用 pipe() 函数向 DataFrame 中的所有元素添加值。
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'])
print('Original DataFrame:\n', df)
df.pipe(adder,2)
print('Modified DataFrame:\n', df)
其输出如下:
Original DataFrame:
col1 col2 col3
0 2.349190 1.908931 -0.121444
1 1.306488 -0.946431 0.308926
2 -0.235694 -0.720602 1.089614
3 0.960508 -1.273928 0.943044
4 -1.180202 -0.959529 0.464541
Modified DataFrame:
col1 col2 col3
0 2.349190 1.908931 -0.121444
1 1.306488 -0.946431 0.308926
2 -0.235694 -0.720602 1.089614
3 0.960508 -1.273928 0.943044
4 -1.180202 -0.959529 0.464541
行或列级函数应用
apply() 函数非常通用,允许您沿 DataFrame 的轴应用函数。默认情况下,它按列应用函数,但您可以使用 axis 参数指定按行应用。
示例:按列应用函数
此示例将函数应用于 DataFrame 列。这里 np.mean() 函数计算每一列的平均值。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 3), columns=['col1', 'col2', 'col3'])
print('Original DataFrame:\n', df)
result = df.apply(np.mean)
print('Result:\n',result)
其输出如下:
Original DataFrame:
col1 col2 col3
0 -0.024666 0.058480 0.658520
1 -0.040997 1.253245 -1.242394
2 1.073832 -1.039897 0.840698
3 0.248157 -1.985475 0.310767
4 -0.973393 -1.002330 -0.890125
Result:
col1 0.056587
col2 -0.543195
col3 -0.064507
dtype: float64
通过将值 1 传递给 axis 参数,可以按行执行操作。
示例:按行应用函数
此函数将 np.mean() 函数应用于 pandas DataFrame 的行。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 3), columns=['col1', 'col2', 'col3'])
print('Original DataFrame:\n', df)
result = df.apply(np.mean, axis=1)
print('Result:\n',result)
其输出如下:
Original DataFrame:
col1 col2 col3
0 0.069495 -1.228534 -1.431796
1 0.468724 0.497217 -0.270103
2 -0.754304 0.053360 -1.298396
3 0.762669 -2.181029 -2.067756
4 0.129679 0.131104 1.010851
Result:
0 -0.863612
1 0.231946
2 -0.666446
3 -1.162039
4 0.423878
dtype: float64
示例:应用 Lambda 函数
以下示例使用 apply() 方法将 lambda 函数应用于 DataFrame 元素。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(5, 3), columns=['col1', 'col2', 'col3'])
print('Original DataFrame:\n', df)
result = df.apply(lambda x: x.max() - x.min())
print('Result:\n',result)
其输出如下:
Original DataFrame:
col1 col2 col3
0 -1.143522 0.413272 0.633881
1 0.200806 -0.050024 0.108580
2 -2.147704 -0.400682 -1.191469
3 2.342222 -2.398639 0.063151
4 -1.071437 1.895879 -0.916805
Result:
col1 4.489926
col2 4.294518
col3 1.825350
dtype: float64
元素级函数应用
当您需要将函数分别应用于每个元素时,可以使用 map() 函数。当函数无法向量化时,这些方法特别有用。
示例:使用 map() 函数
以下示例演示了如何使用 map() 函数将自定义函数应用于 DataFrame 对象的元素。
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) # My custom function df['col1'].map(lambda x:x*100) print(df.apply(np.mean))
其输出如下:
col1 0.480742 col2 0.454185 col3 0.266563 dtype: float64
广告