Search code examples
pythonmatplotlibdata-conversion

Python - ValueError: could not convert string to float


I am a beginner in python and I'm trying to graph some data from a file. The code is the following:

import matplotlib.pyplot as plt
import pandas as pd
from scipy.signal import find_peaks

import os 

dataFrame = pd.read_csv('soporte.txt', sep='\t',skiprows=1, encoding = 'utf-8-sig')


x = dataFrame['Wavelength nm.']
y = dataFrame['Abs.']


indices, _ = find_peaks(y, threshold=1)

plt.plot(x, y)

plt.show()

And I get the following error:

ValueError: could not convert string to float: '-0,04008'

I'll show you a piece of the file I am trying to work with:

"soporte.spc - RawData"
"Wavelength nm."    "Abs."
180,0   -0,04008
181,0   -0,00084
182,0   -0,00746
183,0   0,00854
184,0   -0,01525
185,0   -0,00354

Thank you very much!!!

L


Solution

  • Use the decimal=',' option in pandas, i.e.,

    dataFrame = pd.read_csv('soporte.txt', sep='\t',skiprows=1, encoding = 'utf-8-sig', decimal=',')