Pandas Key Error:0绘制seaborn boxplot

时间:2017-09-27 13:52:40

标签: python pandas seaborn boxplot

我使用以下代码读取Excel文件并使用seaborn包绘制箱线图。

import scipy.stats as sps
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns 
from openpyxl import load_workbook
sns.set()


inpath=r"P:\Data.xlsx"

df=pd.read_excel(io=inpath,header=0,sheetname="65051045")
df1=df[df["Gel.Menge"]!=0]["Gel.Menge"]
print(df1)
fig2=plt.figure(figsize=(15,10))
sns.boxplot(data=df1)
sns.swarmplot(data=df1,color="black",alpha=0.5)
plt.title("65051045")

excel表看起来像:

Gel.Menge   Erf.datum   Freig.
0,000   26.11.2014  26.11.2014
10,000  06.11.2014  07.11.2014
5,000   19.12.2014  08.01.2015
7,000   07.07.2015  17.07.2015
1,000   21.07.2015  22.07.2015
5,000   18.03.2016  22.03.2016
10,000  29.03.2016  31.03.2016
10,000  20.07.2016  21.07.2016
20,000  13.10.2016  17.10.2016
5,000   01.12.2014  01.12.2014
3,000   20.04.2015  20.04.2015

如果我运行代码,则会收到以下错误消息:

  

KeyError Traceback(最近一次调用   最后)in()        84打印(df1)        85 fig2 = plt.figure(figsize =(15,10))   ---> 86 sns.boxplot(data = df1)        87 sns.swarmplot(data = df1,color =“black”,alpha = 0.5)        88 plt.title(“65051045 - LaserschweißenGetriebeabtriebRundnaht”)

     

C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ seaborn \ categorical.py in   boxplot(x,y,hue,data,order,hue_order,orient,color,palette,   饱和度,宽度,fliersize,linewidth,whis,notch,ax,** kwargs)
  2173 plotter = _BoxPlotter(x,y,hue,data,order,hue_order,
  2174东方,颜色,调色板,饱和度,    - > 2175宽度,fliersize,linewidth)2176 2177如果ax是None:

     

C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ seaborn \ categorical.py in    init (自我,x,y,色调,数据,顺序,色调顺序,方向,颜色,调色板,饱和度,宽度,fliersize,线宽)       424宽度,fliersize,linewidth):       425    - > 426 self.establish_variables(x,y,hue,data,orient,order,hue_order)       427 self.establish_colors(颜色,调色板,饱和度)       428

     

C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ seaborn \ categorical.py in   establish_variables(self,x,y,hue,data,orient,order,hue_order,   单位)        94如果hasattr(数据,“形状”):        95如果len(data.shape)== 1:   ---> 96如果np.isscalar(data [0]):        97 plot_data = [数据]        98其他:

     

C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ pandas \ core \ series.py in    getitem (自我,关键)       599 key = com._apply_if_callable(key,self)       600尝试:    - > 601 result = self.index.get_value(self,key)       602       603如果不是is_scalar(结果):

     

C:\ ProgramData \ Anaconda3 \ lib中\站点包\大熊猫\芯\索引\ base.py   在get_value(self,series,key)2426尝试:2427
  return self._engine.get_value(s,k,    - > 2428 tz = getattr(series.dtype,'tz',None))2429除了KeyError   如e1:2430如果len(self)> 0和self.inferred_type in   ['integer','boolean']:

     

pandas._libs.index.IndexEngine.get_value中的pandas_libs \ index.pyx   (pandas_libs \ index.c:4363)()

     

pandas._libs.index.IndexEngine.get_value中的pandas_libs \ index.pyx   (pandas_libs \ index.c:4046)()

     

pandas._libs.index.IndexEngine.get_loc中的pandas_libs \ index.pyx   (pandas_libs \ index.c:5085)()

     

pandas_libs \ hashtable_class_helper.pxi in   pandas._libs.hashtable.Int64HashTable.get_item   (pandas_libs \ hashtable.c:13913)()

     

pandas_libs \ hashtable_class_helper.pxi in   pandas._libs.hashtable.Int64HashTable.get_item   (pandas_libs \ hashtable.c:13857)()

     

KeyError:0

令人惊讶的是,情节(df1)命令正在运行并且它正在绘制:

1     10
2      5
3      7
4      1
5      5
6     10
7     10
8     20
9      5
10     3
Name: Gel.Menge, dtype: int64 

我做错了什么???

1 个答案:

答案 0 :(得分:0)

我认为问题在于我已定义:

df1=df[df["Gel.Menge"]!=0]["Gel.Menge"]

因此df1不再是DataFrame而且seaborn将会混淆。

如果我改变:

df1=df[df["Gel.Menge"]!=0]["Gel.Menge"]

df1["Gel.Menge"]=df["Gel.Menge"].where(df["Gel.Menge"]!=0).dropna()

并明确将df1定义为带有以下内容的数据框:

df1= pd.DataFrame()

代码正常运作。

工作代码如下:

inpath=r"P:\Data.xlsx"
df1=pd.DataFrame()  

df=pd.read_excel(io=inpath,header=0,sheetname="65051045")
df1["Gel.Menge"]=df["Gel.Menge"].where(df["Gel.Menge"]!=0).dropna()
print(df1)
fig2=plt.figure(figsize=(15,10))
sns.boxplot(data=df1)
sns.swarmplot(data=df1,color="black",alpha=0.5)
plt.title("65051045")
相关问题