1

I have a textfile that contains 2 columns of data. They are separated with unfix number of whitespace/s. I want to load it on a pandas DataFrame.

Example:

   306.000000     1.125783
   307.000000     0.008101
   308.000000    -0.005917
   309.000000     0.003784
   310.000000    -0.516513

Please note that it also starts with whitespace/s.

My desired output would be like:

output = {'Wavelength': [306.000000, 307.000000, 308.000000, 309.000000, 310.000000],
          'Reflectance': [1.125783, 0.008101, -0.005917, 0.003784, -0.516513]}

df = pd.DataFrame(data=output)
Nikko
  • 1,410
  • 1
  • 22
  • 49

1 Answers1

3

Use read_csv:

df = pd.read_csv('file.txt', sep='\\s+', names=['Wavelength', 'Reflectance'], header=None)
Code Different
  • 90,614
  • 16
  • 144
  • 163