pandas 钥匙错误:绘制seaborn boxplot时为0 [英] Pandas Key Error: 0 while plotting a seaborn boxplot

查看:144
本文介绍了 pandas 钥匙错误:绘制seaborn boxplot时为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use the following code to read in a Excel file and plot a boxplot using the seaborn package.

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

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

If I run the code I get the following error message:


KeyError Traceback(最近一次调用
最后一次)在()
84 print(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ßenGetriebeabtrieb Rundnaht)

KeyError Traceback (most recent call last) in () 84 print(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ßen Getriebeabtrieb Rundnaht")

C :\ProgramData\Anaconda3\lib\site-packagesseaborn\categorical.py in
boxplot(x,y,hue,data,order, hue_order,orient,color,palette,
饱和度,宽度,fliersize,线宽,whis,缺口,ax,** kwargs)

2173绘图仪= _BoxPlotter(x,y,色相,数据, order,hue_order,

2174方向,颜色,调色板,饱和度,
-> 2175宽度,fliersize,线宽)2176 2177如果ax为None:

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\categorical.py in boxplot(x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, fliersize, linewidth, whis, notch, ax, **kwargs)
2173 plotter = _BoxPlotter(x, y, hue, data, order, hue_order,
2174 orient, color, palette, saturation, -> 2175 width, fliersize, linewidth) 2176 2177 if ax is None:

C:\ProgramData\Anaconda3\lib\站点包\seaborn\categorical.py in
init (self,x,y,hue ,数据,顺序,色相,方向,颜色,调色板,饱和度,宽度,fliersize,线宽)
424宽度,fliersize,线宽):
425
-> 426 self。Establishment_variables( x,y,色相,数据,方向,顺序,色相顺序)
427 self。Establishment_colors(颜色,调色板,饱和度)
428

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\categorical.py in init(self, x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, fliersize, linewidth) 424 width, fliersize, linewidth): 425 --> 426 self.establish_variables(x, y, hue, data, orient, order, hue_order) 427 self.establish_colors(color, palette, saturation) 428

C :\ProgramData\Anaconda3\lib\站点包\seaborn\categorical.py in
Establishment_variables(self,x,y,hue ,数据,方向,顺序,色相,
个单位)
94如果hasattr(data, shape):
95如果len(data.shape)== 1:
---> 96,如果np.isscalar(data [0]):
97 plot_data = [数据]
98否则:

C:\ProgramData\Anaconda3\lib\site-packages\seaborn\categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units) 94 if hasattr(data, "shape"): 95 if len(data.shape) == 1: ---> 96 if np.isscalar(data[0]): 97 plot_data = [data] 98 else:

C: bProgramData\Anaconda3\lib\站点包\pandas\core\series.py in
getitem ((自我,密钥))
599 key = com._apply_if_callable(key,self)
600 try:
-> 601 result = self.index.get_value(self,key)
602
603 if is_scalar(result) ):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in getitem(self, key) 599 key = com._apply_if_callable(key, self) 600 try: --> 601 result = self.index.get_value(self, key) 602 603 if not is_scalar(result):

C:\ProgramData\Anaconda3\lib\站点包\pandas\core\indexes\base.py $ b $ get_value(self,series,key)中的b 2426试试:2427

返回self._engine.get_value(s,k,
-> 2428 tz = getattr(series.dtyp e,'tz',None))2429,但KeyError
如e1:2430如果len(self)> 0且
中有self.inferred_type ['integer','boolean']:

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key) 2426 try: 2427
return self._engine.get_value(s, k, -> 2428 tz=getattr(series.dtype, 'tz', None)) 2429 except KeyError as e1: 2430 if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

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

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

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

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

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

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


中的pandas_libs\hashtable_class_helper.pxi 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:13913)()

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

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

KeyError:0

KeyError: 0

令人惊讶的是plot(df1)命令正在工作并且它绘制:

Suprisingly the plot(df1) command is working and it plots:

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 

我在做什么错??

推荐答案

我认为问题是我有定义:

I assume the issue is that I have defined:

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

因此df1不再是DataFrame,seaborn将会变得混乱。

therewith df1 is no longer a DataFrame and seaborn is going to be confused.

如果我更改:

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

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

,并且还明确定义df1以通过以下方式存储数据框:

and also explicitly define df1 to ba a DataFrame with:

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")

这篇关于 pandas 钥匙错误:绘制seaborn boxplot时为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆