Anaconda 代码:使用 Python 为 Excel 创建和使用用户自定义函数
Owen Price
Owen Price

我们很高兴地宣布 Anaconda Code 中的一项强大的新功能:Python 用户自定义函数!借助 UDF,您可以编写 Python 函数,并像使用原生 Excel 函数一样使用它们,从而将 Python 丰富的数据科学生态系统的全部功能直接带到您的电子表格公式中。让我们看一个真实的例子,展示这种集成有多么强大。
在一次函数调用中将交叉表转换为长格式
考虑一个常见的数据重塑挑战:将交叉制表(宽)数据转换为长格式。您可能会想,“嗯,我可以轻松地使用 Power Query 来做到这一点。” 是的,没错。但是生成的表不是动态的。每次输入数据更改时,都需要刷新查询。并且查询与该工作簿绑定。借助 Anaconda Code 和 Anaconda Toolbox,您可以创建 Python UDF,将其另存为代码片段,并在您打开的任何工作簿中使用它。此外,快速取消透视宽格式表所需的代码非常简单。以下是如何创建一个自定义 UNPIVOT 函数,使其像任何内置 Excel 函数一样工作
# decorate the function with the UDF decorator
@UDF(name='UNPIVOT', nested=False)
def unpivot_data(
data: UDF.Range, # the input data from the Excel sheet
id_vars, # the column(s) to keep fixed in the output
var_name='variable', # the column name to hold the unpivoted column headers
value_name='value' # the column name to hold the unpivoted values
) -> UDF.Range: # output the result as an Excel spilled array
# convert the input data into a Pandas DataFrame
df = to_df(data)
# pass the arguments through to the melt function and return the result
return df.melt(id_vars=id_vars, var_name=var_name, value_name=value_name)
定义后,您可以直接在 Excel 中使用此函数。例如,对于跨年份的销售数据

图 1:UNPIVOT 用户自定义函数直接从 Excel 网格调用
如果您想继续学习此示例,这是一个公式,它将为您提供输入数据。只需将其放入工作表的 B2 单元格中即可。
={"Category",1996,1997,1998;"Confections",29685.549981647,82657.7504128219,55013.9244483569;"Dairy Products",40980.4499421209,115387.639834883,78139.194964755;"Meat/Poultry",28813.6599777699,80975.1079431361,53233.591471141}
设置输入数据后,打开 Anaconda Code 并加载环境。这将确保 Python 环境在加载项中运行

图 2:要在 Anaconda Code 中使用 Python,您首先需要加载环境
加载环境后,转到 Anaconda Code 的导入和定义选项卡,并将 UDF 代码从代码片段粘贴到脚本中。作为参考,这里再次提供
# decorate the function with the UDF decorator
@UDF(name='UNPIVOT', nested=False)
def unpivot_data(
data: UDF.Range, # the input data from the Excel sheet
id_vars, # the column(s) to keep fixed in the output
var_name='variable', # the column name to hold the unpivoted column headers
value_name='value' # the column name to hold the unpivoted values
) -> UDF.Range: # output the result as an Excel spilled array
# convert the input data into a Pandas DataFrame
df = to_df(data)
# pass the arguments through to the melt function and return the result
return df.melt(id_vars=id_vars, var_name=var_name, value_name=value_name)
将代码粘贴到导入和定义脚本后,点击保存并运行按钮以在 Excel 中注册 UNPIVOT 函数。
完成后,绿色的保存并运行按钮将变为运行。这意味着您的函数现在已在 Excel 中注册!继续使用它来取消透视销售数据!
=UNPIVOT(B2:E5,"Category", "Year", "Revenue")
现在您已经了解了如何在 Anaconda Code 中创建 Python 用户自定义函数,这里有一个快速视频,展示了一个更复杂的示例。希望这将激发您对可以使用这项新功能做些什么的想法。
为什么 Anaconda Code 中的 UDF 具有颠覆性
- 原生 Excel 集成:您的 Python 函数像内置 Excel 函数一样工作
- 范围支持:使用 UDF.Range 类型提示自然地处理 Excel 范围
- 灵活的参数:支持带有默认值的可变参数和可选参数
- 内置文档:您的 Python 文档字符串成为 Excel 函数帮助文本
- 本地执行:函数在您安全的 Anaconda Code 环境中本地运行
UDF 入门
创建您自己的 UDF 就像使用 @UDF 装饰器装饰 Python 函数一样简单。以下是它们的特殊之处
- Excel 范围的类型提示:使用 UDF.Range 来处理单元格范围
- 可选参数:添加默认值以使函数参数在 Excel 中成为可选
- 有用的文档:添加文档字符串为 Excel 用户提供函数帮助
- 可变参数:支持 *args 以接受多个输入
立即开始创建您自己的函数
可能性是无限的——从简单的数据转换到复杂的统计分析,您现在可以将 Python 的功能直接带入您的 Excel 公式中。
准备好开始了吗?从 Microsoft AppSource 下载 Anaconda Toolbox,立即开始使用 Python 创建您自己的自定义 Excel 函数!
如果您想提高您的数据可视化技能,请注册我们即将于 2025 年 1 月 23 日举办的直播课程 Excel 中使用 Python 进行数据可视化。在 Owen Price https://www.linkedin.com/in/owenhprice 的这个互动式直播课程中,您将学习如何使用 Excel 中的 Python 和 Anaconda Toolbox for Excel 来制作引人注目的数据可视化。如果您是 付费订阅者,您可以免费参加此直播课程。要注册,请点击此处。
如果您是 Python in Excel 的新手,请查看 我们的点播网络研讨会 Excel 中 Python 简介。在本次网络研讨会中,我们涵盖了 Excel 中 Python 的基础知识、理解数据类型和输出模式、Pandas DataFrames 的简要介绍、自定义函数以及使用 Anaconda Toolbox for Excel 的简化 Python 图表体验。您可以在此处观看网络研讨会。