0

I'm struggling with separating the data from the one and only row I have in a CSV file. While the file has multiple columns the information is on one row and therefore looks like this:

Column 1 | Column 2 | Column 3 ... |Column 628
Seconds  |   X1     |  X2          |Seconds

I just want each 'Seconds' instance to be in the same column as well as each instance of X and Y, so I want the end product to look like this:

Column one   Column 2 Column 3

Seconds      x         y  
Seconds      x         y     
Seconds      x         y     

So far, I've tried using values.reshape to try and structure the data at least a bit but I couldn't find anything on how to extract specific values and put them into new columns. Any input is appreciated!

Smilepest
  • 7
  • 2

1 Answers1

0

So the question is not exactly clear. Please provide a trimmed & sanitized exact example of the source data next time.

From what I understood you have a single-line csv file with repeating instances of seconds, x, y. If this is the case then it's reasonably easy to split this and then process the resulting list.

input csv

seconds1, x1, y1, seconds2, x2, y2, seconds3, x3, y3, seconds4, x4, y4, seconds5, x5, y5

code

You might need to change the delimiter to split on.

import pandas as pd

delimiter = ','
with open("69306802.csv") as infile:
    input_data = infile.read().strip("\n").split(delimiter)


df = pd.DataFrame({
        "seconds": input_data[::3],
        "x": input_data[1::3],
        "y": input_data[2::3]
    })
print(df)

output

     seconds    x    y
0   seconds1   x1   y1
1   seconds2   x2   y2
2   seconds3   x3   y3
3   seconds4   x4   y4
4   seconds5   x5   y5
Edo Akse
  • 4,051
  • 2
  • 10
  • 21
  • I'm sorry, what does the [::3] represent in this case? – Smilepest Sep 23 '21 at 22:46
  • That's list slicing. See [this answer](https://stackoverflow.com/a/509295/9267296) for details. – Edo Akse Sep 23 '21 at 22:50
  • The third variable is step. In this case you basically create a new list for each third element of the original list starting at element 0. The other 2 start at index 1 and 2 respectively. – Edo Akse Sep 23 '21 at 22:51