Max of values from 3 columns in a dataframe?

siddharthagrawal :

I would like to calculate the maximum of 3 columns value.

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)

In data["True _Range"] column, I want to take the maximum of value in data["High-low"],data["Close-low"] and data["Close-high"] columns. On this, it is giving a name error.

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]"

Vaidøtas Ivøška :

Your error signifies that you have input the data instead of the nested columnnames into your view of the dataframe, which produced the error. Since the data from the columns themselves are not in the columnnames - you received the error. One way of avoiding the error is:

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

Sorry to mimick Thaer A's code example, but he did not give an explanation with his, so I believe mine is more relevant now.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=13797&siteId=1