Home > OS >  could not convert string to float with sns.kdeplot
could not convert string to float with sns.kdeplot

Time:01-14

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:

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

CodePudding user response:

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

  • Related