Reputation: 49
I'm trying to plot some graphs with numpy and plotly. I'm getting the data from a csv file containing all the trends that I need. I want to plot and analyze the values contained in columns 1-80 along the timeframe ("Date") Here's the script I've written to do that.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import plotly.express as px
ztec = pd.read_csv(r'C:\Users\tashkpa\Desktop\Работа\ТЭЦ\Сырые данные ЗТЭЦ\данные для python.csv')
ztec=ztec.set_index('Date')
print(ztec)
fig=plt.figure()
ax=fig.add_subplot()
sns.set_style("ticks", {'grid.linestyle': '--'})
sns.lineplot(x=ztec['Date'], y=ztec['01'])
plt.grid(True, which="both", ls="--", c='gray')
plt.plot
A sample of my array looks like this:
'Date';'01';'02';'03';'04';'05';'06';'07';'08';'09';'10';'11';'12';'13';'14';'15';'16';'17';'18';'19';'20';'21';'22';'23';'24';'25';'26';'27';'28';'29';'30';'31';'32';'33';'34';'35';'36';'37';'38';'39';'40';'41';'42';'43';'44';'45';'46';'47';'48';'49';'50';'51';'52';'53';'54';'55';'56';'57';'58';'59';'60';'61';'62';'63';'64';'65';'66';'67';'68';'69';'70';'71';'72';'73';'74';'75';'76';'77';'78';'79';'80';;;;;;;;;;;;;
01/01/2021 02:00:00;0.791;7.019;199.265;183.586;506.253;5.641;41.729;212.521;0.528;0.115;32.356;35.264;0.788;208.541;0.356;0.348;0.087;3.887;3.483;8.784;31.930;13.237;24.296;13.552;23.546;36.840;36.978;218.913;35.373;0.860;0.147;0.319;115.694;0.785;7.064;201.466;183.316;509.751;5.721;42.600;210.235;0.563;12.276;60.000;135.939;0.784;206.678;0.347;0.334;0.066;3.959;4.138;4.821;8.027;13.494;18.367;13.702;18.536;30.446;31.199;243.470;1.908;0.901;2.082;0.216;106.911;3.681;-1.484;205.884;284.659;146.202;3.681;-9.315;56.252;54.483;40195.012;39724.953;102.121;128.471;102.573;;;;;;;;;;;;;
Here's the exception that Python fires.
C:\Users\tashkpa\Anaconda3\python.exe C:/Users/tashkpa/PycharmProjects/pythonProject/zteccsv.py
Traceback (most recent call last):
File "C:/Users/tashkpa/PycharmProjects/pythonProject/zteccsv.py", line 7, in <module>
ztec=ztec.set_index('Date')
File "C:\Users\tashkpa\AppData\Roaming\Python\Python38\site-packages\pandas\core\frame.py", line 4724, in set_index
raise KeyError(f"None of {missing} are in the columns")
KeyError: "None of ['Date'] are in the columns"
How could I bypass this error?
Upvotes: 1
Views: 8885
Reputation: 11
I too received this error, but for me i was trying to set Index as 'Date' where it was already.
So, Once the index is set to a field eg: 'Date', the same code will show the similar error.
Upvotes: 0
Reputation: 2223
pd.read_csv
use a default value for sep=','
sep=';'
when using pd.read_csv
, since the value separator is ;
Date
column as index using ztec=ztec.set_index('Date')
, you have to use the ztec.index
as x
when plotting sns.lineplot(x=ztec.index, y=ztec['01'])
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import plotly.express as px
ztec = pd.read_csv(r'C:\Users\tashkpa\Desktop\Работа\ТЭЦ\Сырые данные ЗТЭЦ\данные для python.csv', sep=';')
ztec=ztec.set_index('Date')
print(ztec)
fig=plt.figure()
ax=fig.add_subplot()
sns.set_style("ticks", {'grid.linestyle': '--'})
sns.lineplot(x=ztec.index, y=ztec['01'])
plt.grid(True, which="both", ls="--", c='gray')
plt.plot()
Upvotes: 1