Home > front end >  Couldn't plot from an excel file with multiple sheets in bokeh with a for loop
Couldn't plot from an excel file with multiple sheets in bokeh with a for loop

Time:04-07

Try to make some plots from an excel file with multiple sheets with bokeh. Wanted to use a for-loop so I don't have to create dataframes over and over again with every sheet. Couldn't get it work. Here are the codes:

from math import pi
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import Range1d,LinearAxis
    

datafile="DataImport.xlsx"

data=pd.ExcelFile(datafile)
sheet_names=data.sheet_names

for sht_name in sheet_names:
    df_sht_name=data.parse(sht_name)
    df_sht_name=sht_name.iloc[2:,[0,2,3]] 

df_sp  #sp is one of sheets in the excel file

Got an error massage "NameError: name 'df_sp' is not defined"

CodePudding user response:

@BigBen The dictionary works. Thanks.

Here are the codes:

datafile="DataImport.xlsx"

data=pd.ExcelFile(datafile)
sheet_names=data.sheet_names


frames={}

for sht_name in sheet_names:
    df_temp=data.parse(sht_name)
    df_temp=df_temp.iloc[2:,[0,2,3]] 
    df_temp.columns=['Date', 'High','Close']
    frames[sht_name]=df_temp

#print(frames)
#print(frames['sp'])

p=figure(y_range=(1000,5000),x_axis_type='datetime', sizing_mode = 'stretch_both') 


 
p.circle(frames['sp']['Date'],frames['sp']['Close'], legend_label='Close',color='red',size=3,alpha=0.8)
  • Related