Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in Python Code when plotting in Seaborn

I am trying to plot distribution graph in python using seaborn . But I am getting error which I am not able to solve given below.

Code:

sns.distplot(df['nn'])

Error:

cannot convert float NaN to integer

Is there any efficient way to solve this by plotting histogram and the distribution?

like image 679
Zhoe Avatar asked Oct 16 '25 15:10

Zhoe


2 Answers

It's because you have missing values in your "nn" variable and seaborn distplot does not handle missing values, you can remove the missing values when plotting:

df = pd.DataFrame([1,1,2,3,4,5,6,6,6,6,np.nan], columns = ["nn"])
sns.distplot(df['nn'].dropna())

This will give you the plot: enter image description here

like image 185
TYZ Avatar answered Oct 18 '25 09:10

TYZ


You provide very little info. However try to skip nan values like

df.dropna(how='all', inplace=True)

More examples: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.dropna.html

like image 21
pcu Avatar answered Oct 18 '25 08:10

pcu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!