Index error - columns vs non-column - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Index error - columns vs non-column (/thread-34478.html) |
Index error - columns vs non-column - Vinny - Aug-03-2021 Dear users, I am getting an IndexError, and not sure why? The code (below) works perfectly when I run it using data.cvs. On that type of file, the index is a column (named Date). But, if I run the code directly from an input library (e.i., df = yf.download(tickers=ticker, start=start, end=end), I receive the trackback message's below. In this example, the date is not considered a column. So, I think the error comes from that; however, I don't know how to fix it. I hope I am being clear. It is my first post, and I am a beginner. Thank you This is my Code: df_close_adj = pd.DataFrame(data=df[["Close", "Adj_Close"]].to_numpy(), index=df["Date"], columns=[["Close", "Adj_Close"]])The Traceback I get is this:
RE: Index error - columns vs non-column - jamesaarr - Aug-05-2021 Hello, I've not used Pandas too much but I believe the error is where you are referencing the index. Index, in case you didn't already know is how the code refers to items on a list or table, for example 0. Apple, 1. Pear, 2. Peach So item 0 is always the first item on the list. Try changing "Date" to the index value of that column. I don't know if that is the solution, however nobody else replied so I thought I would give it a go. Kind regards, James RE: Index error - columns vs non-column - Vinny - Aug-09-2021 Hi James, Thanks for stopping by. I really appreciate your time and effort in trying to help me. Unfortunately, it did not work, BUT I will persuade on my studies, and I hope to figure it out sooner or later. I am a Python beginner. Thank you, and have a great week! Yours sincerely, Vinny (Aug-05-2021, 10:45 AM)jamesaarr Wrote: Hello, RE: Index error - columns vs non-column - snippsat - Aug-09-2021 The error is clear and the problem lay in df Dataframe.When do index=df["Date"] means that the Dataframe most have column named Date.To show a example. import pandas as pd data = { "Name": ["Tom", "nick", "krish", "jack"], "Date": [20, 21, 19, 18], } df = pd.DataFrame(data) >>> df Name Date 0 Tom 20 1 nick 21 2 krish 19 3 jack 18 >>> df['Date'] 0 20 1 21 2 19 3 18 Name: Date, dtype: int64 >>> df.columns Index(['Name', 'Date'], dtype='object')If eg put a space in Date string it will fail with your error. >>> df['Date'] Traceback (most recent call last): ..... KeyError: 'Date' # Date most be the exact name and a column in df >>> df.columns Index(['Name', 'Date '], dtype='object') |