如何创建一个函数来计算 pandas 中数据帧中特定列值的平方?
应该如下所示
def func(数据框,列,值)
最佳答案
假设您有名为 df
的数据框
只需创建一个函数:-
def pow(data,column,val):
return data[column]**val
现在只需调用该函数并传递系列,并将 val 作为参数:-
func(df,'col3',2)
您可以通过以下方式完成此操作而无需创建函数:-
df['column name']**2
关于python - 创建用于数据框的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66457783/