Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

could not convert string to float with sns.kdeplot

I am trying to use sns.kdeplotto get a figure but I get the below error:

ValueError: could not convert string to float: '   0.43082    0.45386'

Do you know how I can fix this error?

Code snippet:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data=pd.read_csv('input.txt', sep="\t", header = None)
sns.kdeplot(data=data, common_norm=False, palette=('b'))

input.txt:

   0.43082    0.45386
   0.35440    0.91632
   0.16962    0.85031
   0.07069    0.54742
   0.31648    1.06689
   0.57874    1.17532
   0.18982    1.01678
   0.31012    0.54656
   0.31133    0.81658
   0.53612    0.50940
   0.36633    0.83130
   0.37021    0.74655
   0.28335    1.30949
   0.11517    0.63141
   0.24908    1.04403
  -0.28633    0.46673
  -0.13251    0.33448
  -0.00568    0.53939
  -0.03536    0.76191
   0.24695    0.92592

>Solution :

It seems that the delimiter of your (.txt) file is not a tab but whitespaces actually. Try this :

data = pd.read_csv("input.txt", sep="\s\s+", engine="python", header=None)

Plot :

plt.figure(figsize=(6, 3))

sns.kdeplot(data=data, common_norm=False, palette=sns.color_palette("rocket", 2));

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading