Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
64 66 81 02 73 -column 1 64 96 34 81 22 - column 2

I have two columns with 5 elements. I want to transpose it.
Expected Output :
64
66
81
02
73 - column 1
61
96
34
81
22 - column 2

What I have tried:

I have tried transposing but I end up getting
64 66 81 02 73 - column 1
61 96 34 81 22 - column 2

But I want this:
66
81
02
73 - column 1
61
96
34
81
22 - column 2
Posted
Updated 10-Mar-22 7:36am
Comments
Richard MacCutchan 10-Mar-22 10:39am    
You need to show your code. We cannot guess what you are doing.
Member 15561848 10-Mar-22 11:08am    
import numpy as np
import pandas as pd
df1 = pd.read_table("A.txt",header=None)
print(df1)
df1_t = df1.T
print(df1_t)
Richard MacCutchan 10-Mar-22 11:17am    
You need to show the output of the two print statements as well as the code. Please use the Improve question link above, and add complete details.
Richard MacCutchan 10-Mar-22 11:54am    
I have made a guess as to the format of the data in your text file, and get the following results:
    0   1   2   3            4
0  64  66  81   2  73-column-1
1  64  96  34  81  22-column-2
             0            1
0           64           64
1           66           96
2           81           34
3            2           81
4  73-column-1  22-column-2
Member 15561848 10-Mar-22 12:23pm    
The text file looks like this:
64 66 81 02 73
64 96 34 81 22
So, it is considering 64 66 81 02 73 as first row and 64 96 34 81 22 as second row. when I transpose, 64 66 81 02 73 64 96 34 81 22. This is what it looks like.

1 solution

Seems you want to use pandas.DataFrame.melt[^], which unpivot a DataFrame from wide to long format, optionally leaving identifiers set. Follow the link for examples.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900