-3

+-------+----------+------+------+
|       |          |      |      |
|   Pid |   Points |   X  |   y  |
|       |          |      |      |
+-------+----------+------+------+
|       |          |      |      |
|   1   |   0      |   10 |   10 |
|       |          |      |      |
+-------+----------+------+------+
|       |          |      |      |
|   1   |   1      |   12 |   12 |
|       |          |      |      |
+-------+----------+------+------+
|       |          |      |      |
|   1   |   2      |   40 |   30 |
|       |          |      |      |
+-------+----------+------+------+
|       |          |      |      |
|   1   |   3      |   31 |   12 |
|       |          |      |      |
+-------+----------+------+------+
|       |          |      |      |
|   1   |   4      |   21 |   24 |
|       |          |      |      |
+-------+----------+------+------+

File Format:

xyz.txt ---->

10 12 40 31 21
10 12 30 12 24

So what I want a simple python script that can select X column values based on Points and insert them into text file as row 1 with space between then. similarly for Y column but place them in row 2.

Can someone help me with this. Thanks

  • This is called ***pivoting***, in pandas package. See [How to pivot a dataframe](https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe) – smci Mar 13 '20 at 07:22

2 Answers2

0

Here is one way to do it. Explanation is in code comments.

import re
s ='''
+-------+----------+------+------+
|       |          |      |      |
|   Pid |   Points |   X  |   Y  |
|       |          |      |      |
+-------+----------+------+------+
|       |          |      |      |
|   1   |   0      |   10 |   10 |
|       |          |      |      |
+-------+----------+------+------+
|       |          |      |      |
|   1   |   1      |   12 |   12 |
|       |          |      |      |
+-------+----------+------+------+
|       |          |      |      |
|   1   |   2      |   40 |   30 |
|       |          |      |      |
+-------+----------+------+------+
|       |          |      |      |
|   1   |   3      |   31 |   12 |
|       |          |      |      |
+-------+----------+------+------+
|       |          |      |      |
|   1   |   4      |   21 |   24 |
|       |          |      |      |
+-------+----------+------+------+
'''
# Find columns based on text
columns = re.findall('[A-Za-z]+', s)
# Find values based on numbers
values = re.findall('\d+', s)
# Get all values in the X column
x = values[columns.index('X'):len(values):len(columns)]
# Get all values in the Y column
y = values[columns.index('Y'):len(values):len(columns)]

# Write the rows to a file
with open('xyz.txt', 'w') as file:
    file.write(' '.join(x) + '\n' + ' '.join(y))
alec
  • 5,799
  • 1
  • 7
  • 20
0

It's simple. A very basic approach is to get you column values in two separate list

list_one = youDataFrame['X'].tolist()
list_two = youDataFrame['y'].tolist()    

with open("your_text.txt", "w") as output:
     output.write(str(list_one))
     output.write('\n')
     output.write(str(list_two))

This will create an output text file in the format that you asked for.

Manojk07
  • 337
  • 2
  • 10