【发布时间】:2021-04-06 06:54:30
【问题描述】:
我试图寻找答案,但找不到。如果有人有链接,那就更好了。我的问题如下。
- 我有一张我正在用 pandas 阅读的表格,其中有许多列,其中包含值。
- 我需要运行三个计算,它们一次使用来自不同列的位。
- 我需要在一段代码中返回这些计算的最大值,然后将其添加到新列中。
我遇到了数字 2 的问题。
这是我的代码的样子。
df = read_csv('file.csv')
df['Get New'] = maximum(df[Long] - df[Short], df[Long] + df[Short], df[Long] / df[Short])
df.to_csv('newFile.csv', index=False)
我知道最大值在这种情况下不起作用,但我似乎找不到什么可以。任何帮助表示赞赏。谢谢!
编辑:这是解决方案。
df['Get New'] = np.maximum(df['Long'] - df['Short'], df['Long'] + df['Short'])
df['Get New'] = np.maximum(df['Get New'], df['Long'] / df['Short'])
【问题讨论】:
标签: pandas dataframe max calculation