0

I got a simple python-made xlsx-sheet with 2 rows.

Something like this:

2   , 23   , 124  , 50 ...

3.3 , 3.44 , 2.67 , 7,3 ...

Is there a way to transponse it, like I can do in excel?

Since it usually contains between 50-100 values - I need a solution which can transpose it no matter how many entries my rows got.

I already tried everything i found on google. But since I am beginner in python I don't seem to be able to get a working solution by myself.

jpp
  • 159,742
  • 34
  • 281
  • 339
DanielHe
  • 179
  • 1
  • 3
  • 10
  • "pivot" is the keyword. https://stackoverflow.com/questions/38067726/pandas-how-to-pivot-one-column-in-rows-into-columns – Stephen J Jul 03 '18 at 14:10

1 Answers1

0

You can use Pandas for this:

import pandas as pd

# define dataframe, or df = pd.read_excel('file.xlsx')
df = pd.DataFrame([[2, 23, 124, 50],
                   [3.3, 3.44, 2.67, 7]])

# transpose and export to excel
df.T.to_excel('out.xlsx', index=False)

Note pd.DataFrame.T is shorthand for the transpose method. Result:

print(df.T)

       0     1
0    2.0  3.30
1   23.0  3.44
2  124.0  2.67
3   50.0  7.00
jpp
  • 159,742
  • 34
  • 281
  • 339