Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Transposing Table
#1
how can i transpose a table in python like below

from

Name Account Amount
Jhon 1 45
Jhon 2 463
Michael 1 76
Michael 2 854
Michael 3 90
Jousef 1 54

To

Name Account#1 Accoucnt#2 Account#3
Jhon 45 463
Michaal 76 854 90
Jousef 54
Quote
#2
What have you tried? Hint: Transposing is the wrong term. You can split the text with splitlines and split.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#3
If this is not an assignment, you can use pandas.
The code would be something like this:

import pandas as pd
data = pd.read_csv('your_file.csv', sep=' ') # check additional options,e.g. header, etc. 
data.T # rotated data
data.T.to_csv('output.csv') # save rotated data to a file
Quote
#4
(Mar-02-2019, 06:35 AM)DeaD_EyE Wrote: What have you tried? Hint: Transposing is the wrong term. You can split the text with splitlines and split.
thanks,

i have tried split but not able to split objects somehow in pandas
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Transposing a Matrix WITHOUT numpy TreasureDragon 5 1,790 Mar-02-2019, 10:48 PM
Last Post: ichabod801
  Transposing in Excel and converting to txt muhsin 2 1,026 Dec-10-2017, 06:06 PM
Last Post: muhsin

Forum Jump:


Users browsing this thread: 1 Guest(s)