python - 数据框中 3 列的最大值?

标签 python

我想计算 3 列值的最大值。

import pandas as pd
import pandas_datareader.data as web
data = web.DataReader("^NSEI" , data_source="yahoo",start = "1/4/2016",end ="6/4/2018")
data=pd.DataFrame(data)
data["High-Low"] = data["High"]  - data["Low"]
data["Close-low"] = abs(data["Close"].shift(1) - data["Low"])
data["Close-High"] = abs(data["Close"].shift(1) - data["High"])
data["True_Range"] = data[[data["High-Low"], data["Close-low"],data["Close-High"]]].max(axis=1)

data["True _Range"] 列中,我想取 data["High-low"] 中的最大值,data[ "Close-low"]data["Close-high"] 列。在此,它给出了一个名称错误。

KeyError: "None of [Index([(156.44970703125, 67.9501953125, 79.75, 118.35009765625, 53.05029296875, 110.75, 100.5, 165.150390625, 161.0, 139.2001953125, 127.25, 98.60009765625, 229.39990234375, 148.7001953125, 105.7998046875, 65.94970703125, 58.19970703125, 59.25, 172.85009765625, 59.2001953125, 148.25, 69.10009765625, 91.099609375, 96.5, 149.349609375, 48.30029296875, 94.10009765625, 248.69970703125, 165.7998046875, 126.0, 166.94970703125, 163.05029296875, 87.25, 80.89990234375, 51.69970703125, 151.0, 81.0498046875, 72.80029296875, 67.7998046875, 268.80029296875, 200.39990234375, 72.2001953125, 77.900390625, 61.7998046875, 85.0, 114.7001953125, 99.7001953125, 83.35009765625, 68.650390625, 92.400390625, 102.85009765625, 105.89990234375, 95.7001953125, 95.849609375, 84.400390625, 56.25, 161.69970703125, 70.64990234375, 98.5, 75.60009765625, 74.0498046875, 60.05029296875, 147.64990234375, 46.89990234375, 94.89990234375, 42.64990234375, 161.94970703125, 54.0498046875, 92.599609375, 77.85009765625, 72.85009765625, 94.35009765625, 50.0, 84.0, 151.9501953125, 50.4501953125, 157.5498046875, 100.349609375, 52.5, 155.10009765625, 51.75, 70.69970703125, 60.5498046875, 120.10009765625, 59.19970703125, 112.2001953125, 66.39990234375, 96.7998046875, 101.75, 60.39990234375, 71.2998046875, 109.400390625, 76.64990234375, 98.39990234375, 45.75, 131.900390625, 134.5, 87.150390625, 49.2001953125, 79.2998046875, ...), (nan, 28.0498046875, 63.44970703125, 184.39990234375, 12.75, 107.0, 76.05029296875, 84.5, 118.60009765625, 109.5, 101.39990234375, 13.14990234375, 193.60009765625, 59.2998046875, 50.80029296875, 1.25, 16.44970703125, 28.14990234375, 21.85009765625, 22.2998046875, 127.900390625, 105.25, 4.150390625, 2.64990234375, 125.89990234375, 112.10009765625, 120.4501953125, 255.75, 107.35009765625, 75.849609375, 125.25, 87.60009765625, 19.39990234375, 45.7998046875, 10.0498046875, 143.849609375, 99.7998046875, 57.30029296875, 14.5, 203.9501953125, 48.05029296875, 85.85009765625, 37.19970703125, 31.5, 43.2001953125, 61.0, 84.39990234375, 25.5498046875, 4.849609375, 85.9501953125, 55.4501953125, 19.35009765625, 5.35009765625, 13.35009765625, 60.4501953125, 44.2998046875, 128.7998046875, 32.85009765625, 46.4501953125, 33.2001953125, 72.2998046875, 8.64990234375, 170.14990234375, 11.4501953125, 78.5, 19.75, 38.35009765625, 8.0498046875, 63.25, 7.7001953125, 37.150390625, 30.64990234375, 38.69970703125, 72.2998046875, 32.5, 22.10009765625, 145.44970703125, 58.5498046875, 72.5, 70.75, 49.75, 0.30029296875, 57.14990234375, 20.099609375, 28.349609375, 106.89990234375, 0.7998046875, 116.19970703125, 42.75, 18.9501953125, 80.0, 103.35009765625, 47.64990234375, 27.5, 15.25, 60.44970703125, 13.60009765625, 7.39990234375, 5.85009765625, 44.2001953125, ...)], dtype='object')] are in the [columns]"

最佳答案

尝试:

data["True_Range"] = data[["High-Low","Close-low","Close-High"]].max(axis=1)

代替:

data["True_Range"] = data[[data["High-Low"], data["Close-low"],data["Close-High"]]].max(axis=1)

关于python - 数据框中 3 列的最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60439030/

相关文章:

Python/MariaDB : Converting . csv 数据到数据框并导出到数据库

python - call_command makemigrations 不适用于 Elastic Beanstalk

Python,余弦相似度与调整后的余弦相似度

python - py2exe - 如何减少 dll 依赖性?

Python-Dymola 接口(interface) : How is it possible to check in real time the progress of a Dymola model?

python - 等待用户输入时运行循环

python - Python Selenium 中的多个滚动条

python - Ubuntu 11.10 上的 VPython 错误

python - 我可以使用数组作为索引对 numpy 数组进行切片吗?

python - Pyspark - 根据一行中的条件过滤掉多行