我们可以将Pandas DataFrame定义为带有一些标记轴(行和列)的二维大小可变的合成表格数据结构。执行算术运算将使行和列标签对齐。可以将其视为Series对象的类似dict的集合。
Pandas DataFrame.transform()函数是自行生成具有转换后值的DataFrame,并且该轴具有与self相同的轴长。
DataFrame.transform(func, axis=0, *args, **kwargs)
func - 是用于转换数据的函数。
axis - 是指0或"index",1或"columns",默认值为0。
*args - 这是一个位置参数,将传递给函子。
** kwargs - 这是一个关键字参数,将传递给函数。
它返回必须与self长度相同的DataFrame。
链接:https://www.learnfk.comhttps://www.learnfk.com/pandas/pandas-dataframe-transform.html
来源:LearnFk无涯教程网
示例1:使用DataFrame.transform()函数向dataframe中的每个元素添加10。
# importing pandas as pd importpandas as pd # Creating the DataFrame info =pd.DataFrame({"P":[8, 2, 9, None, 3], "Q":[4, 14, 12, 22, None], "R":[2, 5, 7, 16, 13], "S":[16, 10, None, 19, 18]}) # Create the index index_ =['A_Row', 'B_Row', 'C_Row', 'D_Row', 'E_Row'] # Set the index info.index =index_ # Print the DataFrame print(info)
输出:
P Q R S A_Row 8.0 4.0 2.0 16.0 B_Row 2.0 14.0 5.0 10.0 C_Row 9.0 12.0 7.0 NaN D_RowNaN 22.0 16.0 19.0 E_Row 3.0NaN 13.0 18.0
示例2:使用DataFrame.transform()函数查找平方根以及对dataframe的每个元素求出的欧拉数的结果。
# importing pandas as pd importpandas as pd # Creating the DataFrame info =pd.DataFrame({"P":[8, 2, 9, None, 3], "Q":[4, 14, 12, 22, None], "R":[2, 5, 7, 16, 13], "S":[16, 10, None, 19, 18]}) # Create the index index_ =['A_Row', 'B_Row', 'C_Row', 'D_Row', 'E_Row'] # Set the index info.index =index_ # Print the DataFrame print(info)
输出:
P Q R S A_Row 88.0 14.0 12.0 16.0 B_Row 12.0 14.0 15.0 10.0 C_Row 19.0 22.0 17.0 NaN D_RowNaN 21.0 16.0 19.0 E_Row 13.0NaN 13.0 18.0
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Python实战 · 从0到1搭建直播视频平台 -〔Barry〕