aozk
aozk

Reputation: 418

seaborn: could not convert string to float

I have DataFrame df:

Timestamp-start    datetime64[ns]
Timestamp-end      datetime64[ns]
M                          object
S                          object
Type                       object
description_x              object
description_y              object
Date               datetime64[ns]
number_col                 float64
dtype: object

number_col:

51      0.0
0       1.0
1       2.0
2       3.0
52      5.0
      ...  
47    148.0
48    148.0
43    148.0
49    149.0
50    149.0
Name: number_col, Length: 132, dtype: float64

And I want to plot histogram using seaborn and column number_col

>     import seaborn as sns
>     
>     sns.distplot(df, x='number_col')

But I got this following error:

ValueError: could not convert string to float: 'number_col'

I have no idea why this is happening, 'number_col' is already a float column.

Upvotes: 0

Views: 13408

Answers (2)

Daweo
Daweo

Reputation: 36390

sns.distplot does expect Series, 1d-array, or list. as 1st argument, not whole pandas.DataFrame, try replacing

sns.distplot(df, x='number_col')

using

sns.distplot(df['number_col'])

single column of pandas.DataFrame is pandas.Series mentioned as Series in snd.distplot's docs

Upvotes: 5

aozk
aozk

Reputation: 418

I found that sns.distplot is deprecated. Instead of that we should use sns.displot.

In first argument of sns.displot we can put pandas.DataFrame:

sns.displot(df, x='number_col')

Upvotes: 3

Related Questions