当使用另一个包含标准偏差的数据帧作为y轴的误差线时,“KeyError 0.0”

2024-07-21 11:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些代码,我无法找到解决方案(尽管有很多谷歌搜索和搜索)。 基本上,我有一个scip,它编译来自同一数据的两个文件-一个文件是六个组在每个波长上的平均值的表,另一个是完全相同的,但包含标准偏差(两个表的大小相同,标题相同,等等)。为平均值绘制文件(df2)会生成图形,这没问题。在

但是,当我试图使用包含标准偏差(df3)值的数据帧作为图形的误差条时,我得到的是“keyror0.0”。 有人能帮我弄清楚出了什么问题吗?非常感谢和感谢。在

我的剧本在下面。在

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

编辑:

这是给我的错误。在

^{pr2}$

EDI2:

i.df2.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

二。df3.head()的输出

            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

Tags: csvtotrueindexpltfilenamepddf1