Python &引用;KeyError 0.0“;当使用另一个包含标准偏差的数据帧作为y轴的误差条时
我有一些代码,我无法找到解决方案(尽管谷歌搜索了很多)。 基本上,我有一个scipt,它编译了两个来自相同数据的文件——一个文件是六组每个波长的平均值表,另一个是完全相同的,但包含标准偏差(两个表大小相同,标题相同,等等)。为平均值绘制文件(df2)生成图形,没有问题 然而,当我尝试使用包含标准偏差(df3)值的数据帧作为图形的误差条时,我得到了“KeyError 0.0”。 有人能帮我找出哪里出了问题吗?非常感谢 我的剧本在下面Python &引用;KeyError 0.0“;当使用另一个包含标准偏差的数据帧作为y轴的误差条时,python,pandas,Python,Pandas,我有一些代码,我无法找到解决方案(尽管谷歌搜索了很多)。 基本上,我有一个scipt,它编译了两个来自相同数据的文件——一个文件是六组每个波长的平均值表,另一个是完全相同的,但包含标准偏差(两个表大小相同,标题相同,等等)。为平均值绘制文件(df2)生成图形,没有问题 然而,当我尝试使用包含标准偏差(df3)值的数据帧作为图形的误差条时,我得到了“KeyError 0.0”。 有人能帮我找出哪里出了问题吗?非常感谢 我的剧本在下面 wave = pd.read_csv("Wavelength_t
wave = pd.read_csv("Wavelength_to_Index_Conversion.csv") #contains a list of wavelengths used to use later on
##
#GET AVERAGE VALUES OF DATA FROM EACH FILE
##
for filename in os.listdir(indir):
print (i)
df1 = pd.read_csv(indir + filename) #imports file
print(filename)
filename = filename[:-12] #removes extra filename and extension
print(filename)
if i == 0:
df2 = pd.DataFrame(data=None, columns=None,index=df1.index)
e = df1['Average']
df2 = df2.assign(temp=e.values)
print (filename)
df2.rename(columns={'temp': str(filename)}, inplace=True)
i = i+1
df2.set_index(wave['Wavelength'], inplace=True)
df2.to_csv("Total_Average.csv")
df2t = df2.T
df2t.to_csv("Total_Average_T.csv")
##
## GET STD DEV
##
i = 0
for filename in os.listdir(indir):
print (i)
df1 = pd.read_csv(indir + filename) #imports file
print(filename)
filename = filename[:-12]
print(filename)
if i == 0:
df3 = pd.DataFrame(data=None, columns=None,index=df1.index)
e = df1['Std Dev']
df3 = df3.assign(temp=e.values)
print (filename)
df3.rename(columns={'temp': str(filename)}, inplace=True)
i = i+1
df3.set_index(wave['Wavelength'], inplace=True)
df3.to_csv("Total_Std Dev.csv")
#plt.rc('font', family='serif', size=13)
#plt.rc('font', family='san-serif', size=13)
#plt.ylim(0, 60)
#plt.xlim(400, 900)
plt.title('Title',fontsize=20)
plt.xlabel('Wavelength (nm)',fontsize=15)
plt.ylabel('Reflectance (%)',fontsize=15)
#plt.autoscale(enable=True, axis=u'both', tight=False)
plt.grid(False)
df2.plot(x = wave['Wavelength'], xlim=(400,850), ylim=(0,60), yerr=df3) #ERROR HERE WHENEVER yerr=df3 IS USED
编辑:
这是我得到的错误
Traceback (most recent call last):
File "<ipython-input-8-d121c6b80d76>", line 1, in <module>
runfile('C:/Users/user/Documents/PhD/Data Manipulation and Graphs/Spectrometry/Plotting All Files into a Graph [X]/Plotting Spectral.py', wdir='C:/Users/user/Documents/PhD/Data Manipulation and Graphs/Spectrometry/Plotting All Files into a Graph [X]')
File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/jdstam/Documents/PhD/Data Manipulation and Graphs/Spectrometry/Plotting All Files into a Graph [X]/Plotting Spectral.py", line 115, in <module>
df2.plot(x = wave['Wavelength'], xlim=(400,850), ylim=(0,60), yerr=df3)
File "C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py", line 3671, in __call__
sort_columns=sort_columns, **kwds)
File "C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py", line 2556, in plot_frame
**kwds)
File "C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py", line 2384, in _plot
plot_obj.generate()
File "C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py", line 987, in generate
self._make_plot()
File "C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py", line 1664, in _make_plot
**kwds)
File "C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py", line 1678, in _plot
lines = MPLPlot._plot(ax, x, y_values, style=style, **kwds)
File "C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py", line 1293, in _plot
return ax.errorbar(x, y, **kwds)
File "C:\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 1811, in inner
return func(ax, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 2963, in errorbar
iterable(yerr[0]) and len(yerr[0]) > 1))):
File "C:\Anaconda3\lib\site-packages\pandas\core\series.py", line 557, in __getitem__
result = self.index.get_value(self, key)
File "C:\Anaconda3\lib\site-packages\pandas\core\index.py", line 3884, in get_value
loc = self.get_loc(k)
File "C:\Anaconda3\lib\site-packages\pandas\core\index.py", line 3942, in get_loc
tolerance=tolerance)
File "C:\Anaconda3\lib\site-packages\pandas\core\index.py", line 1759, in get_loc
return self._engine.get_loc(key)
File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:3979)
File "pandas\index.pyx", line 157, in pandas.index.IndexEngine.get_loc (pandas\index.c:3843)
File "pandas\hashtable.pyx", line 498, in pandas.hashtable.Float64HashTable.get_item (pandas\hashtable.c:9556)
File "pandas\hashtable.pyx", line 504, in pandas.hashtable.Float64HashTable.get_item (pandas\hashtable.c:9494)
KeyError: 0.0
二,。df3.head()的输出
你能显示完整的错误堆栈吗?将添加到main post只是一个猜测:看起来
df2
有一列名为0.0
,而df3
没有。所以我不知道这是否有帮助,但它们都只有“波长,Mon18th_C24_20%,Mon18th_C24_50%,Mon18th_C24_80%,Mon18th_Col0_20%,Mon18th_Col0_50%,Mon18th_Col0_80%”。这两个数据帧还表示它们有6列(索引称为波长)和2048行。感谢您迄今为止的帮助。您可以编辑您的问题以包括df2.head()
和df3.head()
?
Mon18th_C24_20% Mon18th_C24_50% Mon18th_C24_80% Mon18th_Col0_20% Mon18th_Col0_50% Mon18th_Col0_80%
Wavelength
341.514 -1.963704 32.036429 13.413667 -1.396250 39.647 -23.480313
341.892 -1.963704 32.036429 13.413667 -1.396250 39.647 -23.480313
342.269 11.653704 -44.896786 180.915667 -6.503438 38.840 50.237812
342.646 18.801111 305.569286 532.147000 -27.857500 504.351 66.496562
343.024 28.421852 48.280000 65.551000 968.713437 79.782 63.419688
Mon18th_C24_20% Mon18th_C24_50% Mon18th_C24_80% Mon18th_Col0_20% Mon18th_Col0_50% Mon18th_Col0_80%
Wavelength
341.514 103.027866 121.768024 98.039234 60.476486 101.865202 325.329848
341.892 103.027866 121.768024 98.039234 60.476486 101.865202 325.329848
342.269 59.441182 719.315230 643.463142 52.176211 606.273169 177.225557
342.646 41.717983 820.680259 922.506270 73.360823 689.563817 93.132120
343.024 47.129664 75.400200 73.011963 1728.405146 71.020783 52.873068