1
import yfinance as yf
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

df = yf.download('AAPL',start="2021-01-01",end='2025-07-13')
print(df.head()) #display the first five rows of the dataframe
print(df) #to display hte entire dataframe
print(df['Low']) #to select a particular column
print(df.head(122)) #to select a range of rows
print ( " TO CATEGORISE DATA ")
print(df[df['High']>150]) #to categorise data
df['Daily Return'] = df['Close'].pct_change() #to add new column
print(df.head())
print(df.columns) #printing the headers of the columns
df= df.reset_index()
print(df.head())
print(df.columns)
#creating a line plot of 'Close' w.r.t data
print("\nType of 'Date' column:", type(df['Date']))
print("Type of 'Close' column:", type(df['Close']))
sns.lineplot(x='Date',y='Close',data=df)
#df=pd.DataFrame(df)
#df['Close'].plot(kind='line',x='Date',y='Close',title="Representatiion of Close w.r.t. Date")
plt.show()

The problem is with the last few lines of the code. I was expecting a line plot but error it is. How to fix this?

1
  • what error do you get? Don't expect that we will run code to see this error. Always put full error message because there are other useful information. And put it as text, not image - so we could copy it and use in comments, answers or in Google. Commented Jul 13 at 12:40

1 Answer 1

1

Your error was caused by the following code

sns.lineplot(x='Date',y='Close',data=df)

This is because df is multi-indexed. Use the following code instead of the above code

sns.lineplot(data=df.droplevel(1, axis=1), x='Date', y='Close')

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.