Home > OS >  How to manipulate csv entries from horizontal to vertical
How to manipulate csv entries from horizontal to vertical

Time:12-10

I have a csv with the following entries:

apple,orange,bannana,grape
10,5,6,4
four,seven,eight,nine
yes,yes,no,yes
3,5,7,4
two,one,six,nine
no,no,no,yes
2,4,7,8
yellow,four,eight,one
no,yes,no,no

I would like to make a new csv file with the following format and so on:

apple,10,four,yes
orange,5,seven,yes
bannana,6,seven,no
grape,4,nine,yes

I have tried using pandas DataFrames but cant figure how to get the data formatted how I need it.

CodePudding user response:

You can transpose your dataframe in pandas as below.

pd.read_csv('file.csv', index_col=0, header=None).T

this question is already answered:

Can pandas read a transposed CSV?

CodePudding user response:

Hope it works for you.

df = pd.read_csv('<source file name>')
df.T.to_csv('<destination file name>')
  • Related