Skip to content Skip to sidebar Skip to footer

Pandas Key Error: 0 While Plotting A Seaborn Boxplot

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.py

Solution 1:

I assume the issue is that I have defined:

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

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

If I change:

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

to

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

and also explicitly define df1 to ba a DataFrame with:

df1= pd.DataFrame()

the code is working.

The working code looks as follows:

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

Post a Comment for "Pandas Key Error: 0 While Plotting A Seaborn Boxplot"