python中transform用法_Pandas DataFrame.transform用法详解

我们可以将Pandas DataFrame定义为带有一些标记轴(行和列)的二维大小可变的异构表格数据结构。执行算术运算将使行和列标签对齐。可以将其视为Series对象的类似dict的容器。

Pandas DataFrame.transform()函数的主要任务是自行生成具有其转换后的值的DataFrame, 并且它具有与self相同的轴长。

句法:

DataFrame.transform(func, axis=0, *args, **kwargs)

参数:

func:它是用于转换数据的功能。

axis:表示0或’索引’, 1或’列’, 默认值为0。

* args:这是一个位置参数, 将传递给函数。

** kwargs:这是一个关键字参数, 将被传递给函数。

返回值:

它返回必须与self长度相同的DataFrame。

示例1:使用DataFrame.transform()函数向数据框中的每个元素添加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()函数查找平方根, 并将欧拉数的结果提高到数据框的每个元素。

# 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值