# a simple function that takes a number and returns
# 2^n of that number
def calculate_2_n(n):
return 2**n
# use the df.apply method to apply that function to each of the
# cells in the 'Value' column of the DataFrame
df['2_n_value'] = df.apply(lambda row : calculate_2_n(row['Value']), axis = 1)
4条答案
按热度按时间6vl6ewon1#
您可以使用
numpy.power
:或者简单地说,如@* B Remmelzwaal * 所建议的
2 ** df["Value"]
。输出:
kiz8lqtg2#
使用
rpow
:输出:
kqlmhetl3#
可以将
.apply
与lambda函数一起使用在python中,幂运算符是
**
bq9c1y664#
你可以通过使用df.apply方法对 Dataframe 中的每一行应用一个函数。参见this documentation来学习如何使用这个方法。这里有一些未经测试的代码可以帮助你入门。
此代码是this G4G example中代码的修改版本