1

Here is the sample data:

50.2 95.270 51.1 95.260 52.1 95.040 53.0 95.020 54.0 95.020 55.0 95.110 55.9 95.210 56.9 95.080 57.9 95.070

The x-values are 50.2, 51.1, 52.1, etc. The corresponding y-values are 95.270, 95.260, 95.040, etc. Right now these values are all in one row. How can I have python read the x-values in one column and the y-values in another column?

Commas are not in the dataset by the way...

D.Kim
  • 151
  • 3
  • 10

1 Answers1

1

You can create a list with sublists of x values and y values using list comprehension and a slicing trick that grabs every other value in the list starting at a particular index (start at index 0 for x values and index 1 for y values - getting every other value from the starting point). For example:

s = '50.2 95.270 51.1 95.260 52.1 95.040 53.0 95.020 54.0 95.020 55.0 95.110 55.9 95.210 56.9 95.080 57.9 95.070'

data = s.split(' ')    
columns = [data[0:][::2], data[1:][::2]]

print(columns)
# OUTPUT
# [
#     [50.2, 51.1, 52.1, 53.0, 54.0, 55.0, 55.9, 56.9, 57.9],
#     [95.27, 95.26, 95.04, 95.02, 95.02, 95.11, 95.21, 95.08, 95.07]
# ]
benvc
  • 14,448
  • 4
  • 33
  • 54
  • I noticed that you need to add a comma between the numbers. However my data set (which is much larger than the sample data I provided) does not have commas. How can you apply this to a data set with no commas? – D.Kim Dec 04 '18 at 20:18
  • @D.Kim - if your data is just a string of values separated by spaces, then you will need to `str.split()` on a space before applying the slicing trick. Edited answer to demonstrate. – benvc Dec 04 '18 at 20:22